/
NOTES FOR TABLE QUERIES 2 AND SET OPERATORS AND MODIFYING DATA
117 lines (87 loc) · 3.44 KB
/
NOTES FOR TABLE QUERIES 2 AND SET OPERATORS AND MODIFYING DATA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
--SET OPERATORS AND MODIFYING DATA
--Windows Functions Pivot (nonrequired)
--Union
use Community_Assist;
Select PersonFirstName, PersonLastName, PersonEmail --both sides of the union have to have the same structure
From Person --Column names from the top will persist for the whole query
Union
Select EmployeeFirstName, EmployeeLastName, EmployeeEmail --PersonFirstName should match PersonFirstName above
From MetroAlt.dbo.Employee
--intersect
Select PersonAddressCity From PersonAddress
intersect
Select EmployeeCity from MetroAlt.dbo.Employee
--except
Select PersonAddressCity From PersonAddress --only returns cities from the first side
except
Select EmployeeCity from MetroAlt.dbo.Employee
--opposite of upper except
Select EmployeeCity from MetroAlt.dbo.Employee --only returns cities from the first query that are not in the second
except
Select PersonAddressCity From PersonAddress
/*********************
not part of assignment
*********************/
--RANKING FUNCTION
Select GrantRequestKey, GrantTypeKey, GrantRequestAmount,
Row_Number() over (order by GrantRequestAmount desc) as RowNumber,
Rank() over (order by GrantRequestAmount desc) as [Rank],
Dense_Rank() over (order by GrantRequestAmount desc) as [DenseRank],
Ntile(10) over (order by GrantRequestAmount desc) as [NTile]
From GrantRequest
Order by GrantRequestAmount desc
--PARTITION FUNCTIONS
Select GrantRequestKey, GrantTypeKey, GrantRequestAmount,
Sum(GrantRequestAmount) over() as TotalAllocation, --over() means over entire table because we didnt enter anything in parentheses
Sum(GrantRequestAmount) over(partition by GrantTypeKey) as PerType
From GrantRequest
Order by GrantRequestAmount desc
--This version has math to figure the percent of whole and percent of grant type
Select GrantRequestKey, GrantTypeKey, GrantRequestAmount,
Sum(GrantRequestAmount) over() as TotalAllocation, --over() means over entire table because we didnt enter anything in parentheses
Sum(GrantRequestAmount) over(partition by GrantTypeKey) as PerType,
(Sum(GrantRequestAmount) over(partition by GrantTypeKey)/ sum(GrantRequestAmount) over() * 100) as [TypePercentOfWhole],
GrantRequestAmount/
Sum(GrantRequestAmount) over(partition by GrantTypeKey) * 100 as PercentPerType
From GrantRequest
Order by GrantRequestAmount desc
--PIVOT
--for each month, you get how many grants were requested by grant type
--months are X
--Grant type key is Y
--inner query sets up the internal values for the table
Select [Month], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]
From (Select Month(GrantRequestDate) as [Month], GrantTypeKey, GrantRequestKey
From dbo.GrantRequest) as a
Pivot (count(GrantRequestKey) for GrantTypeKey in
([1], [2], [3], [4], [5], [6], [7], [8], [9], [10])) AS b
/****************
INSERTS
****************/
Insert into Person
(PersonLastName, PersonFirstName, PersonEmail, PersonEntryDate)
Values('Simpson', 'Homer', 'Homer@springfield.com', GetDate()),
('Simpson','Marge','Marge@SpringField.com', GetDate())
select * from Person
Create Table Person2
(
LastName nvarchar(255),
FirstName nvarchar(255),
Email nvarchar(255)
)
Insert into Person2(LastName, FirstName, Email)
Select PersonLastName, PersonFirstName, PersonEmail
From Person
select * from Person2
Begin tran
Update Person2
Set FirstName='Jason'
Where Email='JAnderson@gmail.com'
Commit Tran
Begin tran
Update Person2
Set LastName='Smith'
Rollback tran --Reverses what you did since begin tran
Delete from Person2
Where PersonKey=130
Drop Table Person2