-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_Windowing_Functions.sql
More file actions
116 lines (105 loc) · 3.55 KB
/
15_Windowing_Functions.sql
File metadata and controls
116 lines (105 loc) · 3.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
/**************************************************************
* MySQL 8.0 Window Functions Tutorial
* Description: This script demonstrates ranking, navigation,
* distribution, and aggregate window functions in
* MySQL 8.0, along with a median pattern built from
* row numbers when percentile functions are not
* available.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
USE mysql_course;
DROP TABLE IF EXISTS sales;
CREATE TABLE sales
(
sale_id INT PRIMARY KEY,
sale_date DATE NOT NULL,
customer_id INT NOT NULL,
amount DECIMAL(10, 2) NOT NULL
) ENGINE = InnoDB;
INSERT INTO sales (sale_id, sale_date, customer_id, 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);
-------------------------------------------------
-- Region: 1. Ranking Functions
-------------------------------------------------
SELECT
sale_id,
sale_date,
customer_id,
amount,
ROW_NUMBER() OVER (ORDER BY sale_date) AS row_num
FROM sales;
SELECT
sale_id,
sale_date,
customer_id,
amount,
RANK() OVER (ORDER BY amount DESC) AS rank_value,
DENSE_RANK() OVER (ORDER BY amount DESC) AS dense_rank_value,
NTILE(3) OVER (ORDER BY amount DESC) AS ntile_value
FROM sales;
-------------------------------------------------
-- Region: 2. Navigation Functions
-------------------------------------------------
SELECT
sale_id,
sale_date,
customer_id,
amount,
LAG(amount, 1, 0) OVER (ORDER BY sale_date) AS previous_amount,
LEAD(amount, 1, 0) OVER (ORDER BY sale_date) AS next_amount,
FIRST_VALUE(amount) OVER (ORDER BY sale_date) AS first_amount,
LAST_VALUE(amount) OVER (
ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_amount
FROM sales;
-------------------------------------------------
-- Region: 3. Distribution Functions
-------------------------------------------------
SELECT
sale_id,
amount,
CUME_DIST() OVER (ORDER BY amount DESC) AS cume_dist_value,
PERCENT_RANK() OVER (ORDER BY amount DESC) AS percent_rank_value
FROM sales;
-------------------------------------------------
-- Region: 4. Aggregate Window Functions
-------------------------------------------------
SELECT
sale_id,
sale_date,
customer_id,
amount,
SUM(amount) OVER (PARTITION BY customer_id ORDER BY sale_date) AS running_total,
AVG(amount) OVER (PARTITION BY customer_id ORDER BY sale_date) AS running_average,
COUNT(*) OVER (PARTITION BY customer_id ORDER BY sale_date) AS running_count,
MAX(amount) OVER (PARTITION BY customer_id ORDER BY sale_date) AS running_max,
MIN(amount) OVER (PARTITION BY customer_id ORDER BY sale_date) AS running_min
FROM sales;
-------------------------------------------------
-- Region: 5. Median Pattern Without PERCENTILE_CONT
-------------------------------------------------
WITH ordered_sales AS
(
SELECT
amount,
ROW_NUMBER() OVER (ORDER BY amount) AS row_num,
COUNT(*) OVER () AS total_rows
FROM sales
)
SELECT AVG(amount) AS median_amount
FROM ordered_sales
WHERE row_num IN (
FLOOR((total_rows + 1) / 2),
FLOOR((total_rows + 2) / 2)
);
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------