-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.9_Profit_over_time.sql
More file actions
38 lines (38 loc) · 931 Bytes
/
3.9_Profit_over_time.sql
File metadata and controls
38 lines (38 loc) · 931 Bytes
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
WITH division_yearly AS (
SELECT
EXTRACT(YEAR FROM s.order_date)::int AS order_year,
p.division,
SUM(s.gross_profit) AS total_profit
FROM sales s
JOIN products p
ON s.product_id = p.product_id
GROUP BY
EXTRACT(YEAR FROM s.order_date),
p.division
),
division_trends AS (
SELECT
division,
order_year,
total_profit,
LAG(total_profit) OVER (
PARTITION BY division
ORDER BY order_year
) AS prev_year_profit
FROM division_yearly
)
SELECT
division,
order_year,
total_profit,
prev_year_profit,
(total_profit - prev_year_profit) AS change_in_profit,
ROUND(
(total_profit - prev_year_profit)::numeric
/ NULLIF(prev_year_profit, 0)::numeric
, 4) AS pct_change
FROM division_trends
WHERE prev_year_profit IS NOT NULL
ORDER BY
division,
order_year;