Wednesday, January 22, 2014

Joins

--Joins

--inner join, cross, outer joins 

Use Automart

--basic inner join with join syntax
--in the on clause you specify how the two tables
--relate
Select[FirstName],[LastName],[LicenseNumber],[VehicleMake],[VehicleYear]
From Person 
Inner Join Customer.Vehicle
On Person.Personkey=Customer.Vehicle.Personkey 
Order by LastName

--the "inner" key word is the default and not required
--though I think it is a good idea for clarity
Select[FirstName],[LastName],[LicenseNumber],[VehicleMake],[VehicleYear]
From Person 
Join Customer.Vehicle
On Person.Personkey=Customer.Vehicle.Personkey
Order by LastName

--same thing but with the tables aliased
Select p.PersonKey, [FirstName],[LastName],[LicenseNumber],[VehicleMake],[VehicleYear]
From Person p
Join Customer.Vehicle v 
On p.Personkey=v.Personkey
Order by LastName

Select p.PersonKey, [FirstName],[LastName],[LicenseNumber],[VehicleMake],[VehicleYear]
From Person p, Customer.Vehicle v
Where p.Personkey=v.PersonKey

--this results in a cross join
Select p.PersonKey, [FirstName],[LastName],[LicenseNumber],[VehicleMake],[VehicleYear]
From Person p, Customer.Vehicle v

-- two tables joined. You can join as many tables as you need
-- by repeating inner join and on
Select p.PersonKey, [FirstName],[LastName],[LicenseNumber],[VehicleMake],[VehicleYear], email
From Person as p
Inner Join Customer.Vehicle as v 
On p.Personkey=v.Personkey
Inner Join customer.RegisteredCustomer as  rc
on p.Personkey=rc.PersonKey
Order by LastName

--same thing in an older syntax
Select p.PersonKey, [FirstName],[LastName],[LicenseNumber],[VehicleMake],[VehicleYear], email
From Person p, Customer.Vehicle v, Customer.RegisteredCustomer rc
Where p.Personkey=v.PersonKey
And p.FirstName=rc.PersonKey

--explicit cross join
Select person.Personkey, LastName, FirstName, LicenseNumber, VehicleMake, VehicleYear
From Person 
Cross join Customer.Vehicle

--in an left outer join all the records in the first table
--are returned, only the matching records in the second
--table are returned. Where there is no matching record
--the result set displays a null
--outer joins are good for finding mis-matched data
--customers who never purchased anything, for instance

Select ServiceName, Employee.VehicleServiceDetail.AutoServiceID
from Customer.Autoservice
left outer join Employee.VehicleServiceDetail
on Customer.Autoservice.AutoServiceID=Employee.VehicleServiceDetail.AutoServiceID
Where Employee.VehicleServiceDetail.AutoServiceID is null


Select Lastname, rc.Personkey
From Person p
Left outer join Customer.RegisteredCustomer rc
on p.Personkey=rc.PersonKey
Where rc.PersonKey is null

--in a right join its the second table that
--returns all its records
Select LastName, rc.Personkey
From Customer.RegisteredCustomer rc
right outer join Person p
on p.Personkey=rc.PersonKey
Where rc.PersonKey is null

--full join returns all the records from both tables
--same results really as outer join
Select LastName, rc.Personkey
From Customer.RegisteredCustomer rc
full join Person p
on p.Personkey=rc.PersonKey

--just need a record with no matches
Insert into Customer.AutoService(ServiceName, ServicePrice)
Values ('Alternator Replacement', 550.00)

 

Wednesday, January 15, 2014

Notes on Assignment2

using HTTP Get in Asp.net

Here is the code for transferring more than one value via the HTTP query string. The first code is on default2 in the Confirm button.

protected void btnConfirm_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default3.aspx?lastname="+ txtLastName.Text + "&firstname=" + txtFirstName.Text);
    }

The second part, reading the values from the string is in the page load event of Default3

string lastName = Request.QueryString["lastname"];
        string firstName = Request.QueryString["firstname"];
        Label1.Text = "Thank you for submission, " + lastName + ", " + firstName;

Things to think about

The pattern is more important than the detail. You can always look up the details. So what is the patter?

The Master page is a template that lets you create a consistent look and feel for a web site.

When you use a master page, you want to add content pages not web forms. Content pages will use the master pages, web forms are independent.

The assignment is basically moving the content of a form from one page to another. The thing to take away from it, is that it is better to create an object to store all the information (our customer class) and move one thing, rather than move each value separately.

Web pages are stateless, meaning they don't retain values. Each page is also independent of the every other page. Generally, one web page cannot see what's on another. So to move things you have to use a Session variable or a cookie to store the value and then call that session or cookie on the next page. (the same holds true to save a value between page refreshes which happen with each page button click)

Aggregate Functions

--Aggregate functions: count, sum, avg, min, max
use Automart;
--counts all rows
Select count(*) as [number] from Person

Select Count(*) [Number of Employees]From Employee
Where LocationID=1

--aggregate functions ignore nulls
Select count(SupervisorID) [Supervisors] from Employee
Where LocationID=1

Select * from Employee

--
Select Sum(ServicePRice) from Customer.Autoservice
Select avg(ServicePrice) from customer.Autoservice
Select min(ServicePrice) from Customer.Autoservice
Select ServiceName, Max(ServicePrice) from Customer.AutoService
Group by ServiceName

--for future reference, how to get which row matches
--a min or max value. Need to use a subquery
Select ServiceName, ServicePrice
from customer.autoservice
Where ServicePrice = (Select max(servicePrice) From customer.AutoService)

--you must group by any column that is not
--a part of the aggregate function
--in this case, locationID
Select LocationID, count(VehicleServiceID) [total services]
From Employee.VehicleService
Group by LocationID

--the where clause can still be used if the criteria is a 
--simple 'scalar', row by row value. Having is used 
--when the criteria is an aggregate function 
--the where must always come before the group by
--the having always comes after
Select LocationID, month(ServiceDate) [Month], count(VehicleServiceID)[total Services]
From Employee.VehicleService
where LocationID=2
Group by LocationID, month(ServiceDate)
having count(vehicleServiceID) > 5
order by locationID








Thursday, January 9, 2014

Assignment1

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="AssignmentOneStyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Tip Calculator</h1>
        <p>
            <asp:Label ID="Label1" runat="server" Text="Enter the amount">
            </asp:Label> 
            <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>

        </p>
        <asp:RadioButtonList ID="rdbPercentage" runat="server">
            <asp:ListItem Text="10%" Value=".1" />
            <asp:ListItem Text="15%" Value=".15" Selected="true"></asp:ListItem>
            <asp:ListItem Text="20%" Value=".2"></asp:ListItem>
            <asp:ListItem Text="other" Value="0"></asp:ListItem>
        </asp:RadioButtonList> 
        <p><asp:TextBox ID="txtOther" runat="server" placeholder="other"></asp:TextBox></p>
        <asp:Button ID="btnCalculate" runat="server" Text="Submit" OnClick="btnCalculate_Click" />
        <p>
            <asp:Label ID="lblTip" runat="server" Text="" CssClass="result"></asp:Label><br />
            <asp:Label ID="lblTotal" runat="server" Text="" CssClass="result"></asp:Label>
        </p>
    </div>
    </form>
</body>
</html>

   

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        double amount = 0;
        bool goodAmount = double.TryParse(txtAmount.Text, out amount);
        if (!goodAmount)
        {
            Response.Write("<script>alert('Please enter a valid amount');</script>");
            txtAmount.Text = "";
            txtAmount.Focus();
            return;
        }
        double percent=0;
        if(rdbPercentage.SelectedItem.Text != "other")
        { 
            percent = double.Parse(rdbPercentage.SelectedValue.ToString());
        }
        else
        {
            bool goodPercent = double.TryParse(txtOther.Text, out percent);
            if (!goodPercent)
            {
                Response.Write("<script>alert('Please enter a valid Percentage');</script>");
                txtOther.Text = "";
                txtOther.Focus();
                return;
            }
        }
        TipCalculator Tip = new TipCalculator(amount, percent);
        lblTip.Text = Tip.CalculateTip().ToString("$##0.##");
        lblTotal.Text = Tip.CalculateTotal().ToString("$##0.##");
    }
}

CalculateTip.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for TipCalculator
/// 
public class TipCalculator
{

    private double amount;
    private double percent;
 public TipCalculator()
 {
        Amount = 0;
        Percent = 0;
 }
    public TipCalculator(double amt, double perc)
    {
        Amount = amt;
        Percent = perc;
    }


    public double Amount
    {
        set { amount = value; }
        get { return amount; }
    }

    public double Percent
    {
        set
        {
            if (value > 1)
            {
                value /= 100;
            }
            percent = value;
        }
        get
        {
            return percent;
        }
    }

    public double CalculateTip()
    {
        return Amount * Percent;
    }

    public double CalculateTotal()
    {
        return Amount + CalculateTip();
    }
}

AssignmentOne.css

body {
}
h1
{
    background-color:navy;
    color:white;
    border-bottom:5px solid black;
}

.result{
    color:green;
}

Wednesday, January 8, 2014

History scope

Bakery

History

Jenny has been running a bakery for 3 years. She has tried to keep to track of her sales and inventory on a spreadsheet, but has not managed to get clear idea of what her costs and profits if any are. She would like a database to track her real inventory costs as well as point of sale receipts.

Scope

The bakery database will track inventory purchases and usage. It will track waste. It will track sales and labor costs. The database will also track overhead costs. It will allow the owner to compare different types of sales and costs. It will also allow the tracking of customers and customer rewards.

Objective

The database will make the profit/ loss analysis of the bakery more transparent

TimeLine

  • Basic scope and history
  • Gather information—2 weeks
  • Requirements /Business Rules--week
  • Design –week
  • Normalize—testing
  • Build database –add sample data
  • Test it with sql
  • Security and disaster recovery analysis

Simple Selects

Use Automart
/*this is a multiline
comment. this example is 
for assignment 1*/

Select * From Person;

--Simple select listing columns
Select FirstName, LastName 
From Person

Select Lastname, firstName
from Person
order by Lastname 

Select Lastname, Firstname
From person
order by lastname desc, firstname

Select lastname as [Last Name], firstName as [first Name]
From Person

--preview
Select ServiceName, servicePrice, 
cast(Round(ServicePrice * .09, 2)as decimal(5,2)) Tax
From Customer.Autoservice

Select Servicename, servicePrice
From Customer.Autoservice
Where serviceName='tune up'

Select * From Employee.VehicleService
Where ServiceDate='2/25/2010'

Select ServiceName, ServicePrice
From customer.AutoService
Where  ServicePRice   > 100

--<, >, =, !=, >=, <=

Select Distinct VehicleID from Employee.VehicleService
order by VehicleID

Select * from  Employee
Where supervisorID is not null

Select * from  Employee
Where supervisorID is not null
And SupervisorID=6


Select * from  Employee
Where supervisorID is  null
or SupervisorID=6

Select * from Employee.VehicleService
Where ServiceDate between '2/1/2010' and '2/28/2010'

Select GetDate()

Select * from Person
order by lastname

Select * from Person
where lastname like 'C%l'

--% any number of characters
-- _ for one character

Sunday, December 1, 2013

File IO C#

The code for this is on GitHub at https://github.com/spconger/HandlingFiles/tree/master/HandlingFiles

Here are screen shots of the program running

Here is a screen shot of the file in the folder