-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.window-functions.sql
More file actions
150 lines (122 loc) · 4.21 KB
/
03.window-functions.sql
File metadata and controls
150 lines (122 loc) · 4.21 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
-- =====================================================
-- Rank Customers by Total Spending
-- Demonstrates: RANK() over ordered aggregates
-- =====================================================
SELECT c.c_custkey,
c.c_name,
SUM(o.o_totalprice) AS total_spent,
RANK() OVER (
ORDER BY SUM(o.o_totalprice) DESC
) AS spending_rank
FROM customer c
JOIN orders o ON o.o_custkey = c.c_custkey
GROUP BY c.c_custkey, c.c_name
ORDER BY spending_rank;
-- =====================================================
-- Top Order Per Customer
-- Demonstrates: ROW_NUMBER() with PARTITION BY
-- =====================================================
SELECT *
FROM (SELECT o.*,
ROW_NUMBER() OVER (
PARTITION BY o_custkey
ORDER BY o_totalprice DESC
) AS rn
FROM orders o) ranked
WHERE rn < 3;
-- =====================================================
-- Running Total of Sales Over Time
-- Demonstrates: Cumulative SUM()
-- =====================================================
SELECT o_orderdate,
SUM(o_totalprice) AS daily_sales,
SUM(SUM(o_totalprice)) OVER (
ORDER BY o_orderdate
) AS running_total
FROM orders
GROUP BY o_orderdate
ORDER BY o_orderdate;
-- =====================================================
-- Moving Average (7-Day Window)
-- Demonstrates: ROWS frame specification
-- =====================================================
SELECT o_orderdate,
SUM(o_totalprice) AS daily_sales,
AVG(SUM(o_totalprice)) OVER (
ORDER BY o_orderdate
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) AS moving_avg_7d
FROM orders
GROUP BY o_orderdate
ORDER BY o_orderdate;
-- =====================================================
-- Percent Contribution to Total Revenue
-- Demonstrates: Window aggregate without GROUP BY collapse
-- =====================================================
SELECT o_orderkey,
o_totalprice,
o_totalprice / SUM(o_totalprice) OVER () * 100 AS percent_of_total
FROM orders;
-- =====================================================
-- NTILE (Quartiles of Orders)
-- Demonstrates: Data segmentation
-- =====================================================
SELECT o_orderkey,
o_totalprice,
NTILE(4) OVER (
ORDER BY o_totalprice
) AS revenue_quartile
FROM orders;
-- =====================================================
-- LAG() — Compare With Previous Order
-- Demonstrates: Time-series comparison
-- =====================================================
SELECT o_orderkey,
o_orderdate,
o_totalprice,
LAG(o_totalprice) OVER (
ORDER BY o_orderdate
) AS previous_order_value
FROM orders
ORDER BY o_orderdate;
-- =====================================================
-- LEAD() — Compare With Next Order
-- Demonstrates: Forward-looking analytics
-- =====================================================
SELECT o_orderkey,
o_orderdate,
o_totalprice,
LEAD(o_totalprice) OVER (
ORDER BY o_orderdate
) AS next_order_value
FROM orders
ORDER BY o_orderdate;
-- =====================================================
-- Dense Rank Per Region
-- Demonstrates: Partitioned ranking
-- =====================================================
SELECT r.r_name,
c.c_custkey,
SUM(o.o_totalprice) AS total_spent,
DENSE_RANK() OVER (
PARTITION BY r.r_name
ORDER BY SUM(o.o_totalprice) DESC
) AS region_rank
FROM region r
JOIN nation n ON n.n_regionkey = r.r_regionkey
JOIN customer c ON c.c_nationkey = n.n_nationkey
JOIN orders o ON o.o_custkey = c.c_custkey
GROUP BY r.r_name, c.c_custkey
ORDER BY r.r_name, region_rank;
-- =====================================================
-- Window-Based Top 3 Per Customer
-- Demonstrates: Advanced filtering with window logic
-- =====================================================
SELECT *
FROM (SELECT o.*,
ROW_NUMBER() OVER (
PARTITION BY o_custkey
ORDER BY o_totalprice DESC
) AS rn
FROM orders o) t
WHERE rn <= 3;