Wednesday, January 17, 2018

Book ERD

Normalization.

Normalization

--standard white papers

Anomalies Update anomaly--update info in more than one place Insert Anomaly: can't insert one thing because it depends on another, and can't insert the other because it depends on the first (a loop) Deletion Anomaly: If you delete something you unintentionally lose something else.

First normal form

 no repeating groups and no arrays
CD Tracks Track1, Track2, track3
Tracks (My Generation, Substitute, pinball wizard)
anything that can have multiple values, should be seperated out into its own table
--solution
CD(CdID, Title, Year)
Track(TrackID, CDID, TrackTitle)

Second Normal Form

Every entity should be about only one thing.
Should not have any functional dependencies
Remove Blocks of Attributes that relate to each other but not
the main topic (the key)
For example if these attributes were in CD
StudioName, StudioAddress, StudioURL, 

Third normal form

Every entity should be about only one thing.
Every attribute should describe the main thing.
transitive dependency
artist, artistCountry

Tuesday, January 16, 2018

Vincent Vinyl Requirements Morning

Requirements:

  • Track purchases: from who, amount paid, date
  • Album title artist genres
  • Track inventory: what's in stock, condition
  • Track Customers (if willing, or if request)
  • Employees
  • Returns?
  • Sales album sales, What they sold for, Customer,
  • discount percentage
  • Request album date

Business Rules

  • Customer information only if willing
  • or if making request
  • Always get information about person when
  • Purchases 150 % markup though vince can offer discounts
  • Customers who buy 10 albums get a 25%
  • Each album treated individually with its own condition and price

Joins

Use Community_Assist

Select * from Employee
--basic inner join syntax
Select PersonLastName, PersonFirstName,
EmployeeHireDate, EmployeeAnnualSalary
From Person
Inner Join Employee
On Person.PersonKey=Employee.PersonKey

--alternate, older syntax
Select PersonLastName, PersonFirstName,
EmployeeHireDate, EmployeeAnnualSalary
From Person, Employee
Where person.PersonKey=Employee.PersonKey

--cross joins, cartesian join
--old syntax, easy to do by accident
--a cross join matches every record from the first
--table with every record in the second table
Select PersonLastName, PersonFirstName,
EmployeeHireDate, EmployeeAnnualSalary
From Person, Employee
Order by PersonLastName

--new syntax (explicit cross join)
Select PersonLastName, PersonFirstName,
EmployeeHireDate, EmployeeAnnualSalary
From Person 
Cross join Employee
Order by PersonLastName

--multi table joins
Select * from GrantRequest

Select GrantRequestDate, PersonLastName, 
GrantTypeName, GrantRequestAmount
From GrantRequest
inner Join Person
On Person.PersonKey=GrantRequest.Personkey
Inner join GrantType
on GrantType.GrantTypeKey=GrantRequest.GrantTypeKey

--with table alias
Select GrantRequestDate, p.PersonKey, PersonLastName, 
GrantTypeName, GrantRequestAmount
From GrantRequest gr
inner Join Person p
On p.PersonKey=gr.Personkey
Inner join GrantType gt
on gt.GrantTypeKey=gr.GrantTypeKey

--with functions and aggregate functions
Select Year(GrantRequestDate) [Year],
GrantTypeName, Sum(GrantRequestAmount) Total
From GrantRequest gr
inner Join GrantType gt
on gr.GrantTypeKey=gt.GrantTypeKey
Group by Year(GrantRequestDate), GrantTypeName
Order by [Year]

--outer joins
--left outer join returns everything from 
--the first table listed and only matching records
--from the second table
Select Distinct gt.GrantTypeName, gr.GrantTypeKey
From GrantType gt
left outer join GrantRequest gr
on gt.GrantTypeKey=gr.GrantTypeKey
Where gr.GrantTypeKey is null

--right outer join
--same but reversed
Select Distinct gt.GrantTypeName, gr.GrantTypeKey
From GrantRequest gr
right outer join GrantType gt
on gt.GrantTypeKey=gr.GrantTypeKey
Where gr.GrantTypeKey is null

--full join returns everything from both tables
Select Distinct gt.GrantTypeName, gr.GrantTypeKey
From GrantType gt
full join GrantRequest gr
on gt.GrantTypeKey=gr.GrantTypeKey
Where gr.GrantTypeKey is null

Here is the code for updating the MetroAlt ridership table. Download the riders.txt from Canvas files. Right click on MetroAlt, Select TASKS/IMPORT FLAT FILES. Find Riders.Text, just click through until loaded. Then run this insert

Insert into Ridership([BusScheduleAssigmentKey],
[Riders])
Select BusScheduleAssignmentKey, Riders from [Riders]

Then run this Update Code

Use MetroAlt
Go
Alter Table ridership
Add FareKey int
Go
update ridership
set Farekey=1
Where [BusScheduleAssigmentKey] between 1 and 104348
Go
Update Ridership
set FareKey = 2
Where [BusScheduleAssigmentKey] between  104349 and 235028
Go
Update Ridership
set FareKey = 3
Where [BusScheduleAssigmentKey] between 235029 and 365708
Go
Update Ridership
set FareKey = 4
Where FareKey is Null

Tuesday, January 9, 2018

Selects Functions

Use Community_Assist
--math
Select 5 * 2 /3 % 2

Select DonationAmount,
DonationAmount * .80 as Charity,
DonationAmount * .20 as Maintenance
from Donation

Select DonationAmount,
DonationAmount * .80  Charity,
DonationAmount * .20  Maintenance
from Donation;
--scaler
--Date Functions
Select * from Donation;
Select Distinct Year(DonationDate) [Year] from Donation;
Select Distinct Month(DonationDate) [Month] from Donation;
Select Distinct Day(DonationDate) [Day] from Donation;
Select DonationDate, DatePart(Minute,DonationDate) as [Minute] from Donation;

Select * from Donation
Where Year(DonationDate)=2015
And Month(donationDate) between 8 and 9;

Select GetDate()
Select * from Employee
Select DateDiff(Month,'2/21/2005','4/21/2013')[Difference]
Select DateAdd(day, 13,GetDate())

--formats
Select * from Donation
Select DonationDate, Format(DonationAmount,'$#,##0.00') [DonationAmount] from Donation
Select * from Contact
Select '(' + substring(ContactNumber,1,3) + ')' + 
substring(ContactNumber,4,3) 
+ '-' + substring(ContactNumber, 7,4) [Phone] from Contact

Select ContactNumber, 
Format(Cast(ContactNumber as bigint),
'(000)000-0000') from Contact
--Aggregate --operate across multiple rows
Select sum(DonationAmount) Total from Donation
Select Avg(DonationAmount) Total from Donation
Select count(DonationAmount) Total from Donation
Select Max(DonationAmount) Total from Donation
Select Min(DonationAmount) Total from Donation

Select Year(DonationDate) [Year],
format(Sum(DonationAmount),'$#,##0.00') Total
From Donation
Group by Year(DonationDate)

Select Year(DonationDate) [Year], Month(DonationDate) [Month],
format(Sum(DonationAmount),'$#,##0.00') Total
From Donation
Group by Year(DonationDate), Month(DonationDate)
Order by Year(DonationDate),Month(DonationDate)

Select Year(DonationDate) [Year],
Case Month(DonationDate)
   when 8  then 'August'
   when 9  then 'September'
   when 2 then 'February'
   when 4 then 'April'
end  as [Month],
DonationAmount
From Donation

Select Year(donationDate) [Year],
datename(Month,donationDate) [Month],
DonationAmount
From Donation

Select name from Sys.Databases
Select name from sys.Tables

I forgot to cover this in class. If you have an aggregate function in the criteria, you have to use "having" instead of "where"

--You must use HAVING with any criteria that uses an
--aggregate function
--these examples have both a having and a where clause
Select EmployeeKey, AVG(DonationAmount) Average
From Donation
Where EmployeeKey > 2
Group by EmployeeKey
Having AVG(DonationAmount) < 1000

Monday, January 8, 2018

Identifying the problem
Interview with main stakeholder
What kinds of things use it for: 
Help desk: Inventory of computers (where, who, what), printers, and related hardware, supplies
Users, faculty and staff,
Open Tickets--Tickets--What computer, what the problem is, who is assigned (Techs) resolution, date
Process for assigning and solving problems
Interview other stakeholders: faculty and staff, techs, 
Job shadowing
Review Documents--forms, Reports, manuals and procedures
Pick out all the nouns
Computer, Printer, Inventory, Ticket, Software, Techs, Users(faculty, staff) 
Computer--make, manufacturer, model, CSC@,Location
Tech--name, specialty, phone, email, group
Ticket--Date, user, problem, status, solutionDate, computer, Tech Assigned

Sample stuff

Model class

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

namespace WebApplication2.Models
{
    public class ModelTest
    {
        public int ID { get; set; }

        public string ItemName { get; set; }

        public double Price { get; set; }

        

    }
}

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class TestController : Controller
    {
        // GET: Test
        public ActionResult Index()
        {
            ModelTest mt = new ModelTest();
            mt.ID = 1;
            mt.ItemName = "item1";
            mt.Price = 25.00;
            ModelTest mt1 = new ModelTest();
            mt1.ID = 2;
            mt1.ItemName = "item2";
            mt1.Price = 5.00;
            ModelTest mt2 = new ModelTest();
            mt2.ID = 3;
            mt2.ItemName = "item3";
            mt2.Price = 13.00;
            List<ModelTest> items = new List<ModelTest>();
            items.Add(mt);
            items.Add(mt1);
            items.Add(mt2);

            return View(items);
        }
    }
}

the web page

@model  IEnumerable<WebApplication2.Models.ModelTest>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<table class="table">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Price</th>
    </tr>
    @foreach (var item in Model)
    {
    <tr>
        <td>@Html.DisplayFor(model => item.ID)</td>
        <td>@Html.DisplayFor(model => item.ItemName)</td>
        <td>@Html.DisplayFor(model => item.Price)</td>
    </tr>
    }
</table>