-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_Windowing_Functions.sql
More file actions
210 lines (188 loc) · 5.91 KB
/
15_Windowing_Functions.sql
File metadata and controls
210 lines (188 loc) · 5.91 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
/**************************************************************
* SQL Server 2022 Windowing Functions Tutorial
* Description: This script demonstrates various windowing
* (analytic) functions in SQL Server, including:
* - ROW_NUMBER(), RANK(), DENSE_RANK(), NTILE()
* - LAG(), LEAD(), FIRST_VALUE(), LAST_VALUE()
* - CUME_DIST(), PERCENT_RANK()
* - PERCENTILE_CONT() and PERCENTILE_DISC()
* - Aggregate window functions (SUM, AVG, COUNT, MAX, MIN)
* along with the use of PARTITION BY and ORDER BY.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Ensure you are using the target database.
*/
USE TestDB;
GO
-------------------------------------------------
-- Region: 1. Creating the Sales Table and Inserting Data
-------------------------------------------------
/*
1.1 Create a sample table to demonstrate window functions.
*/
IF OBJECT_ID(N'dbo.Sales', N'U') IS NOT NULL
DROP TABLE dbo.Sales;
GO
CREATE TABLE dbo.Sales
(
SaleID INT PRIMARY KEY,
SaleDate DATE,
CustomerID INT,
Amount DECIMAL(10, 2)
);
GO
/*
1.2 Insert sample sales data.
*/
INSERT INTO dbo.Sales (SaleID, SaleDate, CustomerID, Amount)
VALUES
(1, '2023-01-01', 1, 100.00),
(2, '2023-01-02', 2, 150.00),
(3, '2023-01-03', 1, 200.00),
(4, '2023-01-04', 3, 250.00),
(5, '2023-01-05', 2, 300.00);
GO
-------------------------------------------------
-- Region: 2. Ranking Functions
-------------------------------------------------
/*
2.1 ROW_NUMBER() - Assigns a unique number to each row ordered by SaleDate.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
ROW_NUMBER() OVER (ORDER BY SaleDate) AS RowNum
FROM dbo.Sales;
GO
/*
2.2 RANK() - Assigns a rank to each row, with gaps for ties.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
RANK() OVER (ORDER BY Amount DESC) AS Rank
FROM dbo.Sales;
GO
/*
2.3 DENSE_RANK() - Similar to RANK() but without gaps.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
DENSE_RANK() OVER (ORDER BY Amount DESC) AS DenseRank
FROM dbo.Sales;
GO
/*
2.4 NTILE() - Distributes rows into a specified number of groups.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
NTILE(3) OVER (ORDER BY Amount DESC) AS NTile
FROM dbo.Sales;
GO
-------------------------------------------------
-- Region: 3. Navigation Functions
-------------------------------------------------
/*
3.1 LAG() - Accesses data from the previous row.
If no previous row exists, default to 0.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
LAG(Amount, 1, 0) OVER (ORDER BY SaleDate) AS PrevAmount
FROM dbo.Sales;
GO
/*
3.2 LEAD() - Accesses data from the next row.
If no subsequent row exists, default to 0.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
LEAD(Amount, 1, 0) OVER (ORDER BY SaleDate) AS NextAmount
FROM dbo.Sales;
GO
/*
3.3 FIRST_VALUE() - Returns the first value in the window.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
FIRST_VALUE(Amount) OVER (ORDER BY SaleDate) AS FirstAmount
FROM dbo.Sales;
GO
/*
3.4 LAST_VALUE() - Returns the last value in the window.
The window frame must cover all rows.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
LAST_VALUE(Amount) OVER (ORDER BY SaleDate
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LastAmount
FROM dbo.Sales;
GO
-------------------------------------------------
-- Region: 4. Distribution Functions
-------------------------------------------------
/*
4.1 CUME_DIST() - Cumulative distribution of a value in a set.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
CUME_DIST() OVER (ORDER BY Amount DESC) AS CumeDist
FROM dbo.Sales;
GO
/*
4.2 PERCENT_RANK() - Relative rank of a row within a group.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
PERCENT_RANK() OVER (ORDER BY Amount DESC) AS PercentRank
FROM dbo.Sales;
GO
-------------------------------------------------
-- Region: 5. Percentile Functions
-------------------------------------------------
/*
5.1 PERCENTILE_CONT() - Calculates a continuous percentile (e.g., median).
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY Amount) OVER () AS MedianAmount
FROM dbo.Sales;
GO
/*
5.2 PERCENTILE_DISC() - Calculates a discrete percentile.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY Amount) OVER () AS MedianAmount
FROM dbo.Sales;
GO
-------------------------------------------------
-- Region: 6. Aggregate Window Functions with PARTITION BY
-------------------------------------------------
/*
6.1 SUM() - Running total of Amount for each Customer.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
SUM(Amount) OVER (PARTITION BY CustomerID ORDER BY SaleDate) AS RunningTotal
FROM dbo.Sales;
GO
/*
6.2 AVG() - Running average of Amount for each Customer.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
AVG(Amount) OVER (PARTITION BY CustomerID ORDER BY SaleDate) AS RunningAvg
FROM dbo.Sales;
GO
/*
6.3 COUNT() - Running count of sales for each Customer.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
COUNT(*) OVER (PARTITION BY CustomerID ORDER BY SaleDate) AS RunningCount
FROM dbo.Sales;
GO
/*
6.4 MAX() - Running maximum sale amount for each Customer.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
MAX(Amount) OVER (PARTITION BY CustomerID ORDER BY SaleDate) AS RunningMax
FROM dbo.Sales;
GO
/*
6.5 MIN() - Running minimum sale amount for each Customer.
*/
SELECT SaleID, SaleDate, CustomerID, Amount,
MIN(Amount) OVER (PARTITION BY CustomerID ORDER BY SaleDate) AS RunningMin
FROM dbo.Sales;
GO
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------