-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-window-functions.sql
More file actions
403 lines (359 loc) · 11.4 KB
/
Copy path11-window-functions.sql
File metadata and controls
403 lines (359 loc) · 11.4 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
-- ============================================================
-- SQL Masterclass — Chapter 11: Window Functions
-- ============================================================
-- 🔴 ADVANCED
--
-- In this chapter you will learn:
-- • What window functions are and why they're powerful
-- • ROW_NUMBER, RANK, DENSE_RANK
-- • NTILE for bucketing
-- • LAG and LEAD for accessing adjacent rows
-- • Cumulative SUM, AVG with window frames
-- • PARTITION BY for grouped windows
-- • Moving averages and running totals
-- ============================================================
-- ============================================================
-- 11.1 WHAT ARE WINDOW FUNCTIONS?
-- ============================================================
-- Window functions perform calculations across rows related
-- to the current row WITHOUT collapsing them into groups.
--
-- Syntax: function() OVER (
-- PARTITION BY ... -- optional: groups rows
-- ORDER BY ... -- ordering within each partition
-- ROWS BETWEEN ... -- optional: window frame
-- )
-- ============================================================
-- 11.2 ROW_NUMBER — Unique sequential numbering
-- ============================================================
-- Number all sellers by total revenue
WITH seller_revenue AS (
SELECT
seller_id,
SUM(price) AS total_revenue
FROM order_items
GROUP BY seller_id
)
SELECT
ROW_NUMBER() OVER (ORDER BY total_revenue DESC) AS rank_num,
seller_id,
total_revenue
FROM seller_revenue
LIMIT 15;
-- Number each item within an order
SELECT
order_id,
order_item_id,
product_id,
price,
ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY price DESC) AS item_rank
FROM order_items
LIMIT 20;
-- ============================================================
-- 11.3 RANK and DENSE_RANK
-- ============================================================
-- RANK: Ties get the same rank, next rank is skipped.
-- DENSE_RANK: Ties get the same rank, next rank is NOT skipped.
-- Rank product categories by total revenue
WITH category_revenue AS (
SELECT
p.product_category_name,
SUM(oi.price) AS total_revenue
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id
WHERE p.product_category_name IS NOT NULL
GROUP BY p.product_category_name
)
SELECT
RANK() OVER (ORDER BY total_revenue DESC) AS rank_pos,
DENSE_RANK() OVER (ORDER BY total_revenue DESC) AS dense_rank_pos,
product_category_name,
total_revenue
FROM category_revenue
LIMIT 15;
-- Top seller per state (using RANK + PARTITION BY)
WITH seller_state_revenue AS (
SELECT
s.seller_state,
s.seller_id,
SUM(oi.price) AS total_revenue
FROM order_items oi
JOIN sellers s ON oi.seller_id = s.seller_id
GROUP BY s.seller_state, s.seller_id
),
ranked AS (
SELECT
seller_state,
seller_id,
total_revenue,
RANK() OVER (PARTITION BY seller_state ORDER BY total_revenue DESC) AS state_rank
FROM seller_state_revenue
)
SELECT *
FROM ranked
WHERE state_rank <= 3
ORDER BY seller_state, state_rank;
-- ============================================================
-- 11.4 NTILE — Divide into equal buckets
-- ============================================================
-- Divide customers into 4 spending quartiles
WITH customer_spending AS (
SELECT
o.customer_id,
SUM(p.payment_value) AS total_spent
FROM orders o
JOIN order_payments p ON o.order_id = p.order_id
GROUP BY o.customer_id
)
SELECT
NTILE(4) OVER (ORDER BY total_spent) AS spending_quartile,
customer_id,
total_spent
FROM customer_spending
LIMIT 20;
-- Summarize quartiles
WITH customer_spending AS (
SELECT
o.customer_id,
SUM(p.payment_value) AS total_spent
FROM orders o
JOIN order_payments p ON o.order_id = p.order_id
GROUP BY o.customer_id
),
quartiles AS (
SELECT
NTILE(4) OVER (ORDER BY total_spent) AS quartile,
total_spent
FROM customer_spending
)
SELECT
quartile,
COUNT(*) AS num_customers,
MIN(total_spent) AS min_spend,
AVG(total_spent) AS avg_spend,
MAX(total_spent) AS max_spend
FROM quartiles
GROUP BY quartile
ORDER BY quartile;
-- ============================================================
-- 11.5 LAG and LEAD — Access adjacent rows
-- ============================================================
-- LAG(col, n) → value from n rows BEFORE current row
-- LEAD(col, n) → value from n rows AFTER current row
-- Month-over-month revenue comparison
WITH monthly_revenue AS (
SELECT
SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7) AS year_month,
SUM(oi.price) AS revenue
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7)
)
SELECT
year_month,
revenue,
LAG(revenue, 1) OVER (ORDER BY year_month) AS prev_month_revenue,
revenue - LAG(revenue, 1) OVER (ORDER BY year_month) AS revenue_change,
CASE
WHEN LAG(revenue, 1) OVER (ORDER BY year_month) > 0
THEN ROUND(
100.0 * (revenue - LAG(revenue, 1) OVER (ORDER BY year_month))
/ LAG(revenue, 1) OVER (ORDER BY year_month), 1
)
ELSE NULL
END AS growth_pct
FROM monthly_revenue
ORDER BY year_month;
-- ============================================================
-- 11.6 CUMULATIVE SUM — Running totals
-- ============================================================
-- Running total of monthly revenue
WITH monthly_revenue AS (
SELECT
SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7) AS year_month,
SUM(oi.price) AS revenue
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7)
)
SELECT
year_month,
revenue,
SUM(revenue) OVER (
ORDER BY year_month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS cumulative_revenue
FROM monthly_revenue
ORDER BY year_month;
-- Cumulative orders per state
WITH monthly_state_orders AS (
SELECT
c.customer_state,
SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7) AS year_month,
COUNT(DISTINCT o.order_id) AS order_count
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
GROUP BY c.customer_state, SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7)
)
SELECT
customer_state,
year_month,
order_count,
SUM(order_count) OVER (
PARTITION BY customer_state
ORDER BY year_month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS cumulative_orders
FROM monthly_state_orders
WHERE customer_state IN ('SP', 'RJ', 'MG')
ORDER BY customer_state, year_month;
-- ============================================================
-- 11.7 MOVING AVERAGES — Smoothing trends
-- ============================================================
-- 3-month moving average of revenue
WITH monthly_revenue AS (
SELECT
SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7) AS year_month,
SUM(oi.price) AS revenue
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7)
)
SELECT
year_month,
revenue,
AVG(revenue) OVER (
ORDER BY year_month
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS moving_avg_3m
FROM monthly_revenue
ORDER BY year_month;
-- ============================================================
-- 11.8 PERCENT OF TOTAL — Using SUM OVER ()
-- ============================================================
-- Each state's share of total revenue
WITH state_revenue AS (
SELECT
c.customer_state,
SUM(oi.price) AS total_revenue
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY c.customer_state
)
SELECT
customer_state,
total_revenue,
SUM(total_revenue) OVER () AS grand_total,
ROUND(100.0 * total_revenue / SUM(total_revenue) OVER (), 2) AS pct_of_total
FROM state_revenue
ORDER BY total_revenue DESC;
-- Each seller's share within their state
WITH seller_rev AS (
SELECT
s.seller_state,
s.seller_id,
SUM(oi.price) AS revenue
FROM order_items oi
JOIN sellers s ON oi.seller_id = s.seller_id
GROUP BY s.seller_state, s.seller_id
)
SELECT
seller_state,
seller_id,
revenue,
SUM(revenue) OVER (PARTITION BY seller_state) AS state_total,
ROUND(100.0 * revenue / SUM(revenue) OVER (PARTITION BY seller_state), 2) AS pct_of_state
FROM seller_rev
WHERE seller_state IN ('SP', 'RJ')
ORDER BY seller_state, revenue DESC
LIMIT 20;
-- ============================================================
-- EXERCISES
-- ============================================================
-- Exercise 1: Rank sellers by the number of distinct orders
-- they've fulfilled. Show top 10.
-- Exercise 2: For each month, calculate the month-over-month
-- change in the number of orders (not revenue).
-- Exercise 3: Create a cumulative sum of payment values by
-- payment type over months.
-- Exercise 4: Divide all sellers into 5 equal groups (NTILE)
-- based on their total revenue. Show the average
-- revenue per group.
-- ============================================================
-- SOLUTIONS
-- ============================================================
-- Exercise 1
WITH seller_orders AS (
SELECT
seller_id,
COUNT(DISTINCT order_id) AS num_orders
FROM order_items
GROUP BY seller_id
)
SELECT
RANK() OVER (ORDER BY num_orders DESC) AS seller_rank,
seller_id,
num_orders
FROM seller_orders
LIMIT 10;
-- Exercise 2
WITH monthly_orders AS (
SELECT
SUBSTR(CAST(order_purchase_timestamp AS VARCHAR), 1, 7) AS year_month,
COUNT(*) AS order_count
FROM orders
GROUP BY SUBSTR(CAST(order_purchase_timestamp AS VARCHAR), 1, 7)
)
SELECT
year_month,
order_count,
LAG(order_count) OVER (ORDER BY year_month) AS prev_month,
order_count - LAG(order_count) OVER (ORDER BY year_month) AS change
FROM monthly_orders
ORDER BY year_month;
-- Exercise 3
WITH monthly_payments AS (
SELECT
SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7) AS year_month,
p.payment_type,
SUM(p.payment_value) AS monthly_value
FROM order_payments p
JOIN orders o ON p.order_id = o.order_id
GROUP BY SUBSTR(CAST(o.order_purchase_timestamp AS VARCHAR), 1, 7), p.payment_type
)
SELECT
year_month,
payment_type,
monthly_value,
SUM(monthly_value) OVER (
PARTITION BY payment_type
ORDER BY year_month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS cumulative_value
FROM monthly_payments
WHERE payment_type IN ('credit_card', 'boleto')
ORDER BY payment_type, year_month;
-- Exercise 4
WITH seller_rev AS (
SELECT
seller_id,
SUM(price) AS total_revenue
FROM order_items
GROUP BY seller_id
),
tiled AS (
SELECT
NTILE(5) OVER (ORDER BY total_revenue) AS quintile,
total_revenue
FROM seller_rev
)
SELECT
quintile,
COUNT(*) AS num_sellers,
MIN(total_revenue) AS min_rev,
AVG(total_revenue) AS avg_rev,
MAX(total_revenue) AS max_rev
FROM tiled
GROUP BY quintile
ORDER BY quintile;