Tuesday, January 30, 2018

First SQL

--Select, Join, inserts
--fourth generation language
--2nd generation assembly
--3rd generation Cobol C C++ C# Python procedural print
--fourth generation how, what to do

use Community_Assist

Select * from Person

Select PersonFirstName, 
PersonLastName, 
PersonEmail
From Person
Order by PersonLastName;

Select PersonFirstName, 
PersonLastName, 
PersonEmail
From Person
Order by PersonLastName desc

Select * from PersonAddress
Where PersonAddressCity='Kent'

Select * from PersonAddress
Where PersonAddressCity='Seattle'

Select * from PersonAddress
Where Not PersonAddressCity='Seattle'

Select * from PersonAddress
Where PersonAddressApt is Not null

Select * from Donation 
where DonationDate between'8/10/2015' and '8/11/2015'

Select Count(DonationKey) as [Count] from Donation
Where DonationDate between'8/10/2015' and '8/11/2015'

Select sum(DonationAmount) as Total from Donation
Where DonationDate between'8/10/2015' and '8/11/2015'

Select * from Donation

Select PersonFirstName, PersonLastName,
PersonEmail, DonationDate, DonationAmount
From Person
inner join Donation
On Person.PersonKey=Donation.PersonKey

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

Insert into Person(PersonLastName, 
PersonFirstName, PersonEmail,  
PersonEntryDate)
Values('Phalan','Declan','pd@gmail.com',getDate())

Insert into PersonAddress(PersonAddressApt, 
PersonAddressStreet, 
PersonAddressCity, PersonAddressState, 
PersonAddressZip, PersonKey)
Values(null,'1234 South','Marysville','WA', '98100', 
IDENT_CURRENT('Person'))

Select * from PersonAddress

Insert into Donation(PersonKey, DonationDate, 
DonationAmount)
Values(130,getDate(),5.00)

Select * from Position

Insert into Position(PositionName, PositionDescription)
Values('Professor','Teaches economics'),
('HR', 'Human Resources Manager'),
('Advertising', 'Publicize the charity')

Set Operators and Modifying Data

--set operators--

--union
Use metroAlt

Select EmployeeLastName lastname, EmployeeFirstName firstname, 
EmployeeEmail email, EmployeeCity City
From Employee
Union
Select PersonLastName, PersonFirstName, PersonEmail, PersonAddressCity
From Community_Assist.dbo.person p
inner join Community_Assist.dbo.PersonAddress pa
on p.PersonKey=pa.PersonKey

Select EmployeeCity City 
From Employee
Intersect
Select PersonAddressCity
From Community_Assist.dbo.PersonAddress


Select PersonAddressCity city
From Community_Assist.dbo.PersonAddress
Intersect
Select EmployeeCity  
From Employee

Select PersonAddressCity city
From Community_Assist.dbo.PersonAddress
Except
Select EmployeeCity  
From Employee

Select EmployeeCity  
From Employee
Except
Select PersonAddressCity city
From Community_Assist.dbo.PersonAddress

--Modifying Data--
Use Community_Assist

Insert into Person(PersonLastName, PersonFirstName, 
PersonEmail, PersonPassWord, PersonEntryDate, 
PersonPassWordSeed)
Values('Jordan','Michael','mj@gmail.com',null,GetDate(),null)

Insert into PersonAddress(PersonAddressApt, 
PersonAddressStreet, 
PersonAddressCity, PersonAddressState, 
PersonAddressZip, PersonKey)
Values(Null,'10002 Broad Street','Charlotte','NC', '90110', IDENT_CURRENT('Person'))

Insert into Donation(PersonKey, DonationDate, 
DonationAmount)
values(3, getDate(),100),
(109, getDate(),400),
(20, getDate(),250)

Create table Person2
(
PersonKey int, 
PersonLastName nvarchar(255), 
PersonFirstName nvarchar(255), 
PersonEmail nvarchar(255), 
PersonEntryDate DateTime
)

Insert into Person2(PersonKey, PersonLastName, 
PersonFirstName, PersonEmail, PersonEntryDate)
Select PersonKey, PersonLastName, 
PersonFirstName, PersonEmail, PersonEntryDate
From Person

Update Person2
Set PersonFirstName='Jason'
Where personKey = 1

Update Person2
set PersonlastName='Manning',
PersonEmail = 'LManning@gmail.com'
Where PersonKey=3

Begin tran

Update Person2
Set PersonLastName='Smith'


Select * from Person2
rollback tran
Commit tran

Truncate table Person2

Begin tran

Delete from Person2 where Personkey=130

Rollback tran

Drop table Person2

Select * from GrantType

Update Granttype
Set GrantTypeMaximum=GrantTypeMaximum * 1.05


Update Granttype
Set GrantTypeLifetimeMaximum=GrantTypeLifetimeMaximum * 1.05

Monday, January 29, 2018

NewPersonClass

Add this to your model and use it for the registration

    public class NewPerson
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string PlainPassword { get; set; }
        public string Apartment{ get; set; }
        public string Street{ get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zipcode { get; set; }


    }
}

Thursday, January 25, 2018

Table Expressions (Queries in the FROM), VIews

Use MetroAlt

Select Year(BusScheduleAssignmentDate) [Year], Sum(riders) Annual,
Avg(riders) Average,
(Select sum(riders) From Ridership) Total,
(Cast(Sum(riders)as decimal(10,2))/(Select sum(riders) From Ridership))*100 [Percent]
From ridership
Inner Join BusScheduleAssignment
on ridership.BusScheduleAssigmentKey=BusScheduleAssignment.BusScheduleAssignmentKey
Group by Year(BusScheduleAssignmentDate)
Order by Year

--table expressions
Use Community_Assist

Select LastName, FirstName, Email, City
From (Select PersonLastName [LastName],
PersonFirstName FirstName,
PersonEmail Email,
PersonAddressCity City
From Person
Inner join PersonAddress
on Person.PersonKey=PersonAddress.PersonKey
Where PersonAddressCity='Bellevue') as BellevueResidents

Select RequestMonth, GrantTypeName, count(GrantTypekey) as [Count] From
   (Select month(GrantRequestDate) RequestMonth, sg.GrantTypeKey, GrantTypeName
   From GrantRequest sg
   inner join GrantType cs
   on sg.GrantTypeKey=cs.GrantTypeKey) as ServiceCount
   Group by RequestMonth, GrantTypeName
   Order by RequestMonth
   
--Common table expressions
with BellevueResidents as
(
   Select PersonLastName LastName,
   PersonFirstName FirstName,
   PersonEmail Email,
   PersonAddressCity City
   From Person
   Inner Join PersonAddress
   On person.PersonKey=PersonAddress.PersonKey
   where PersonAddressCity='Bellevue'
)
Select LastName, FirstName, Email, City
From BellevueResidents;

with ServiceCount as
(
 Select month(GrantRequestDate) RequestMonth, sg.GrantTypeKey, GrantTypeName
   From GrantRequest sg
   inner join GrantType cs
   on sg.GrantTypeKey=cs.GrantTypeKey
)
Select RequestMonth, GrantTypeName, Count(GrantTypeKey) [Count]
From ServiceCount
Group by RequestMonth, GrantTypeName
Order by RequestMonth;

Declare @City Nvarchar(255)='Kent';
--Set @City='Kent';

with BellevueResidents as
(
   Select PersonLastName LastName,
   PersonFirstName FirstName,
   PersonEmail Email,
   PersonAddressCity City
   From Person
   Inner Join PersonAddress
   On person.PersonKey=PersonAddress.PersonKey
   where PersonAddressCity=@City
)
Select LastName, FirstName, Email, City
From BellevueResidents;
go

create function fx_CityFunction
(@City nvarchar(255))
returns table
As
return
   Select PersonLastname , 
   PersonFirstName ,
   PersonEmail ,
   PersonAddressCity
   From Person
   inner join PersonAddress
   on person.PersonKey = PersonAddress.PersonKey
   Where PersonAddressCity=@City


Select * from dbo.fx_CityFunction('Shoreline')

go
Create view Vw_HumanResouces
As
Select PersonFirstName [First name],
PersonLastname [Last Name],
PersonEmail [Email],
EmployeeHireDate [Hire Date],
EmployeeAnnualSalary [Annual Salary],
PositionName [Position]
From Person
Inner Join Employee
On Person.PersonKey=Employee.PersonKey
Inner Join EmployeePosition
On Employee.EmployeeKey = EmployeePosition.Employeekey
inner join Position
on Position.PositionKey=EmployeePosition.PositionKey

go

Select * From Vw_HumanResouces
Where Position='Associate'

Select [Last Name] from Vw_HumanResouces

Go
Alter view Vw_HumanResouces with Schemabinding
As
Select PersonFirstName [First name],
PersonLastname [Last Name],
PersonEmail [Email],
EmployeeHireDate [Hire Date],
EmployeeAnnualSalary [Annual Salary],
PositionName [Position]
From dbo.Person
Inner Join dbo.Employee
On dbo.Person.PersonKey=dbo.Employee.PersonKey
Inner Join dbo.EmployeePosition
On dbo.Employee.EmployeeKey = dbo.EmployeePosition.Employeekey
inner join dbo.Position
on dbo.Position.PositionKey=dbo.EmployeePosition.PositionKey

go
Begin tran
Alter table Person
Drop column PersonEmail
Rollback tran

Monday, January 22, 2018

Vince's Requirements (1 PM Class)

Requirements for Vince's Database
Album(AlbumID, Title, Year, studio)<=> Artist(ArtistID Artistname)
Track inventory (InventoryID, AlbumID, condition, pricepaid, DateAquired, Sale Price)
Record Purchases (vince purchasing)
Employees (employeeId, name, email)
Sales(SaleNumber, Date, EmployeeID, CustomerID (null))
SaleDetail(SaleDetailId, SaleNumber, InventoryID, Discount)
Customer(CustomerID, CustomerName, CustomerEmail)
Requests(Albumid, Date, CustomerID)

Requirements--something the database has to do to work successfully
Business Rules 150%, 25% discount for 10


Adding Github to Visual Studio

  1. Launch Visual Studio
  2. Go to TOOLS on the Menu
  3. Choose EXTENSIONS AND UPDATES
  4. In the Dialog Box click ONLINE
  5. In the search bar type "Github"
  6. Choose GITHUB EXTENSIONS FOR VISUAL STUDIO
  7. Download them
  8. Close Dialog and Visual Studio
  9. Installer will come up
  10. Say yes to allow changes
  11. MODIFY
  12. This can take a couple of minutes
  13. When done close dialog and open Visual Studio
  14. In the START window under OPEN click GITHUB
  15. Enter your github user name and password
  16. Close dialog
  17. Start a new project
  18. There should be a toolbar at the bottom of Visual Studio with the name, master and an up arrow with commits
  19. Click the up arrow.
  20. Publish to GitHub

Thursday, January 18, 2018

SubQueries

Use Community_Assist

Select Max(DonationAmount) [Max]
From Donation

-- simple subquery in where clause
Select PersonFirstName, PersonLastName, DonationAmount
from person 
inner join Donation 
on person.PersonKey=donation.PersonKey
Where DonationAmount = (Select Max(donationAmount) from Donation)

Select PersonAddressStreet, PersonAddressCity, 
PersonKey, PersonAddressZip
From PersonAddress
where PersonAddressCity in ('Bellevue', 'Kent', 'Shoreline')

Select PersonLastName, PersonfirstName, PersonEmail
From person
Where Personkey in (Select personkey from Employee)

Select PersonLastName, PersonfirstName, PersonEmail
From person
Where Personkey in 
(Select personkey from Employee
where EmployeeKey in 
(Select EmployeeKey from GrantReview))

Select GrantTypeName 
from GrantType
Where GrantTypeKey not in 
(Select GrantTypeKey from GrantRequest)

--sub queries in the select
Select GrantTypeName, 
Sum(GrantRequestAmount) Total,
(Select sum(GrantRequestAmount) From GrantRequest) GrantTotal,
(Sum(GrantRequestAmount) / 
    (Select Sum(GrantRequestAmount) from GrantRequest)) * 100  [Percent]
From GrantType
inner join GrantRequest
on GrantType.GrantTypeKey=GrantRequest.GrantTypeKey
Group by GrantTypeName

--Coordinated subqueries are queries in which the subquery 
--references the outer query. The subquery matches
--the granttypekey from gr1, the outer query with the granttypeKey
--from gr2 inside the subquery. The effect is to make sure
--that like is matched with like. GrantTypeKey 1 (Food) is matched 
--only against other granttypeKey 1 values, GrantTypeKey 2 (Rent)
--is matched only against other grantTypeKey 2 values, etc.
--a coordinated subquery is the SQL equivalent of a recursive
--function in other programming languages, and like them  
-- requires a lot of processor time 


Select GrantTypeKey, GrantRequestAmount
From GrantRequest gr1
Where GrantRequestAmount > 
(Select avg(GrantRequestAmount)
from GrantRequest gr2
Where gr1.GrantTypeKey=gr2.GrantTypeKey)

Select GrantTypeKey, avg(GrantRequestAmount) Average from GrantRequest
Group by GrantTypeKey
use Community_Assist
Create table PersonZ
(
  PersonKey int primary key,
  Lastname nvarchar(255),
  FirstName nvarchar(255),
  Email nvarchar(255)
)

Insert into PersonZ(PersonKey, Lastname, FirstName, Email)
Select [PersonKey],[PersonLastName],[PersonFirstName],[PersonEmail]
From Person
Where PersonLastName Like 'Z%'

Select * From Personz