Repository navigation
Expand file tree
/
Copy path03-advanced.sql
More file actions
102 lines (97 loc) · 2.29 KB
/
Copy path03-advanced.sql
File metadata and controls
102 lines (97 loc) · 2.29 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
-- Lesson 03: Advanced SQL
-- Goal: learn CTEs, window functions, ranking, and running totals.
-- 1. Monthly sales with a running total.
WITH monthly_sales AS (
SELECT
p.year_actual,
p.month_actual,
p.month_name,
SUM(s.sales) AS total_sales
FROM superstore s
JOIN periode p
ON s.order_date = p.date_actual
GROUP BY p.year_actual, p.month_actual, p.month_name
)
SELECT
year_actual,
month_actual,
month_name,
total_sales,
SUM(total_sales) OVER (
ORDER BY year_actual, month_actual
) AS running_sales
FROM monthly_sales
ORDER BY year_actual, month_actual;
-- 2. Profit ranking by category.
WITH product_profit AS (
SELECT
category,
product_name,
SUM(profit) AS total_profit
FROM superstore
GROUP BY category, product_name
)
SELECT
category,
product_name,
total_profit,
DENSE_RANK() OVER (
PARTITION BY category
ORDER BY total_profit DESC
) AS profit_rank
FROM product_profit
ORDER BY category, profit_rank, product_name;
-- 3. Top-selling month in each year.
WITH monthly_sales AS (
SELECT
p.year_actual,
p.month_actual,
p.month_name,
SUM(s.sales) AS total_sales
FROM superstore s
JOIN periode p
ON s.order_date = p.date_actual
GROUP BY p.year_actual, p.month_actual, p.month_name
),
ranked_months AS (
SELECT
year_actual,
month_actual,
month_name,
total_sales,
ROW_NUMBER() OVER (
PARTITION BY year_actual
ORDER BY total_sales DESC
) AS rn
FROM monthly_sales
)
SELECT
year_actual,
month_name,
total_sales
FROM ranked_months
WHERE rn = 1
ORDER BY year_actual;
-- 4. Previous order date by customer.
SELECT
customer_id,
customer_name,
order_date,
sales,
LAG(order_date) OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS previous_order_date
FROM superstore
ORDER BY customer_id, order_date;
-- 5. High-discount, low-profit products.
SELECT
sub_category,
product_name,
AVG(discount) AS avg_discount,
SUM(profit) AS total_profit
FROM superstore
GROUP BY sub_category, product_name
HAVING AVG(discount) > 0.3
AND SUM(profit) < 0
ORDER BY avg_discount DESC, total_profit;