-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path19_WindowFunctions_Demo.sql
More file actions
214 lines (170 loc) · 4.55 KB
/
Copy path19_WindowFunctions_Demo.sql
File metadata and controls
214 lines (170 loc) · 4.55 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/******************************************************************************
Course videos: https://www.red-gate.com/hub/university/courses/t-sql/tsql-for-beginners
Course scripts: https://litknd.github.io/TSQLBeginners
Ranking, Numbering, and Running Totals with Windowing Functions
EXAMPLES FOR STUDY
*****************************************************************************/
/* ✋🏻 Doorstop ✋🏻 */
RAISERROR ( N'Did you mean to run the whole thing?', 20, 1 ) WITH LOG ;
GO
/******************************************************************************
Set up sample data
*****************************************************************************/
USE master ;
GO
IF DB_ID ( 'TSQLSchool' ) IS NOT NULL DROP DATABASE TSQLSchool ;
GO
CREATE DATABASE TSQLSchool ;
GO
USE TSQLSchool ;
GO
--DROP IF EXISTS is SQL Server 2016 SP3+
DROP TABLE IF EXISTS dbo.doggos
GO
CREATE TABLE dbo.doggos
(dogid INT IDENTITY,
doggo VARCHAR(128) NOT NULL,
age TINYINT NOT NULL,
sex CHAR(1) NOT NULL,
CONSTRAINT pk_doggos PRIMARY KEY CLUSTERED(dogid)) ;
GO
INSERT dbo.doggos(doggo, age, sex)
VALUES
('Mister', 10, 'M'),
('Stormy', 3, 'M'),
('Wendell', 5, 'M'),
('Kota', 10, 'F'),
('Fletcher', 4, 'M'),
('Scout', 2, 'F');
GO
/******************************************************************************
Let's run some queries!
*****************************************************************************/
--ROWNUMBER
--SQL Server 2008+
--https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql
SELECT
ROW_NUMBER () OVER (ORDER BY age DESC) AS rownum,
doggo,
age,
sex
FROM dbo.doggos;
GO
--You must have an ORDER BY clause for row_number,
--but it can be garbage 🗑 if you simply want arbitrary numbering
SELECT
ROW_NUMBER () OVER (ORDER BY (SELECT 1/0) ) AS rownum,
doggo,
age,
sex
FROM dbo.doggos;
GO
--We can partition! We can also use multiple columns for ordering
SELECT
ROW_NUMBER () OVER ( PARTITION BY sex ORDER BY age DESC, doggo) AS rownum,
sex,
doggo,
age
FROM dbo.doggos;
GO
--RANK and DENSE_RANK handle ties - compare
--SQL Server 2008+
SELECT
RANK () OVER (ORDER BY age DESC) AS ranking,
doggo,
age,
sex
FROM dbo.doggos;
SELECT
DENSE_RANK () OVER (ORDER BY age DESC) AS denseranking,
doggo,
age,
sex
FROM dbo.doggos;
GO
--We can partition as well when ranking
SELECT
sex,
DENSE_RANK () OVER (PARTITION BY sex ORDER BY age DESC) AS denseranking,
doggo,
age
FROM dbo.doggos;
GO
--AVG by sex
--We learned this in GROUP BY
SELECT
sex,
AVG(age) AS WrongAvgDueToDataType,
AVG(1.0 * age) AS AvgAgeBySex
FROM dbo.doggos
GROUP BY sex
GO
--What if we want to display the avg by gender for each row?
--WE CAN DO THAT
SELECT
sex,
doggo,
age,
AVG(1.0 * age) OVER (PARTITION BY sex ) AS AvgAgeBySex,
AVG(1.0 * age) OVER ( ) AS AvgAgeOverall
FROM dbo.doggos
ORDER BY sex;
GO
--let's add in median, why not?
--"PERCENTILE_CONT interpolates the appropriate value,
--whether or not it exists in the data set, while PERCENTILE_DISC always returns an actual value from the set."
--SQL Server 2012+
--https://docs.microsoft.com/en-us/sql/t-sql/functions/percentile-disc-transact-sql
SELECT
sex,
doggo,
age,
AVG(1.0 * age) OVER (PARTITION BY sex ) AS avgagebysex,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY age ) OVER (partition by sex) AS medianagebysex,
AVG(1.0 * age) OVER ( ) AS avgageoverall,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY age ) OVER () AS medianageoverall
FROM dbo.doggos
ORDER BY sex;
GO
--max and count can use windows, as well
SELECT
sex,
doggo,
age,
max(age) OVER ( PARTITION BY sex ) AS MaxAgeBySex,
max(age) OVER ( ) AS MaxAgeOverall,
count(age) OVER ( PARTITION BY sex ) AS CountWithThisSex,
count(age) OVER ( ) AS CountOverall
FROM dbo.doggos
ORDER BY sex;
GO
--oh, let's talk running totals
--SUM with a window
--Look at the last two rows in this one, what happened?
SELECT
doggo,
age,
SUM(age) OVER (ORDER BY age) AS agerunningtotal,
SUM(age) OVER () AS agetotal
FROM dbo.doggos
ORDER BY age;
--Compare
SELECT
dogid,
doggo,
age,
SUM(age) OVER (ORDER BY dogid) AS agerunningtotal,
SUM(age) OVER () AS agetotal
FROM dbo.doggos
ORDER BY dogid;
GO
--running total with partitions
SELECT
doggo,
sex,
age,
SUM(age) OVER (PARTITION BY sex ORDER BY age) AS agerunningtotalforsex,
SUM(age) OVER (PARTITION BY sex) AS agetotalforsex
FROM dbo.doggos
ORDER BY sex, age;
GO