-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.6_ Sales Trends.sql
More file actions
42 lines (38 loc) · 1.16 KB
/
Copy path3.6_ Sales Trends.sql
File metadata and controls
42 lines (38 loc) · 1.16 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
-- A. Monthly Sales & Profit Trends
WITH monthly AS (
SELECT
DATE_TRUNC('month', order_date)::date AS period_start,
SUM(total_sales) AS sales,
SUM(gross_profit) AS profit,
SUM(units) AS units_sold
FROM sales
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY period_start
)
SELECT
period_start,
sales,
profit,
units_sold
FROM monthly
WHERE period_start >= DATE_TRUNC('month', NOW())::date - INTERVAL '24 months';
-- B. Quarterly Sales & Profit Trends
-- Quarterly Sales & Profit Trends (last 8 quarters = last 24 months)
WITH quarterly AS (
SELECT
DATE_TRUNC('quarter', order_date)::date AS period_start,
SUM(total_sales) AS sales,
SUM(gross_profit) AS profit,
SUM(units) AS units_sold
FROM sales
GROUP BY DATE_TRUNC('quarter', order_date)
ORDER BY period_start
)
SELECT
period_start,
sales,
profit,
units_sold
FROM quarterly
WHERE period_start >= DATE_TRUNC('quarter', NOW())::date
- INTERVAL '24 months';