-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReports_for_BI.sql
More file actions
189 lines (169 loc) · 5.16 KB
/
Reports_for_BI.sql
File metadata and controls
189 lines (169 loc) · 5.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
--============================================================================
-- Customer Report
--============================================================================
Purpose:
- This report consolidates key customer metrics and behaviors
Highlights:
1. Gathers essential fields such as names, ages, and transaction details.
2. Segments customers into categories (VIP, Regular, New) and age groups.
3. Aggregates customer-level metrics:
- total orders
- total sales
- total quantity purchased
- total products
- lifespan (in months)
4. Calculates valuable KPIs:
- recency (months since last order)
- average order value
- average monthly spend
==========================================================================
*/
CREATE VIEW gold.report_customers AS
WITH details AS (
-- 1) retrieve core columns from the table
SELECT
CONCAT(c.first_name, ' ' ,c.last_name) full_name,
EXTRACT(year FROM AGE(current_DATE, c.birth_date)) AS age,
order_date,
f.sales_amount,
f.quantity,
f.order_number,
f.product_key,
c.customer_number
FROM
gold.fact_sales f
LEFT JOIN gold.dim_customers c
ON f.customer_key = c.customer_key
LEFT JOIN gold.dim_products p
ON f.product_key = p.product_key
WHERE order_date IS NOT NULL
)
-- 2) customer aggregations: summerizing key metrics
, customer_aggregations AS (
SELECT
full_name,
customer_number,
product_key,
age,
SUM(sales_amount) total_sales,
COUNT(DISTINCT order_number) total_orders,
SUM(quantity) total_quantity,
MAX(order_date) last_order,
EXTRACT (month FROM AGE(MAX(order_date), MIN(order_date))) AS life_span
FROM
details
GROUP BY full_name, customer_number,product_key, age
)
SELECT
full_name,
customer_number,
product_key,
age,
CASE WHEN age < 21 then 'teen'
WHEN age between 21 and 45 THEN'young'
WHEN age > 45 THEN 'old'
END age_segments,
CASE WHEN total_sales > 5000 AND life_span >= 10 THEN 'VIP'
WHEN total_sales <= 5000 AND life_span >= 10 THEN 'Regular'
WHEN life_span < 10 AND total_sales < 1000 THEN 'new'
END customer_segments,
total_sales,
total_orders,
total_quantity,
(DATE_PART('year', AGE(current_DATE, last_order)) * 12)
+ DATE_PART('month', AGE(current_DATE, last_order)) AS recency,
CASE WHEN total_orders = 0 THEN 0
ELSE total_sales / total_orders
END AS average_order_value, -- (AVO)
CASE WHEN life_span = 0 THEN 0
ELSE ROUND(total_sales / life_span)
END AS average_monthly_spending
FROM
customer_aggregations
/*
--============================================================================
Product Report
--============================================================================
Purpose:
- This report consolidates key product metrics and behaviors.
Highlights:
1. Gathers essential fields such as product name, category, subcategory, and cost.
2. Segments products by revenue to identify High-Performers, Mid-Range, or Low-Performers.
3. Aggregates product-level metrics:
- total orders
- total sales
- total quantity sold
- total customers (unique)
- lifespan (in months)
4. Calculates valuable KPIs:
- recency (months since last sale)
- average order revenue (AOR)
- average monthly revenue
============================================================
*/
CREATE VIEW gold.report_products AS
WITH product_details AS (
SELECT
f.customer_key,
f.order_number,
f.product_key,
p.category,
p.subcategory,
p.product_name,
f.sales_amount,
p.product_cost,
f.quantity,
f.order_date
FROM
gold.fact_sales f
LEFT JOIN
gold.dim_products p
ON p.product_key = f.product_key
)
, aggregation_product AS (
SELECT
category,
subcategory,
product_name,
COUNT(DISTINCT order_number) total_orders,
COUNT(DISTINCT customer_key) total_customers,
SUM(sales_amount) total_sales,
ROUND(AVG(product_cost)) average_cost,
ROUND(AVG(sales_amount/ NULLIF(quantity,0))) average_price,
MAX(order_date) last_order,
(DATE_PART('year', AGE(MAX(order_date), MIN(order_date))) * 12)
+ DATE_PART('month', AGE(MAX(order_date), MIN(order_date))) AS life_span
FROM
product_details
GROUP BY
category,
subcategory,
product_name
)
SELECT
category,
subcategory,
product_name,
CASE
WHEN total_sales > 30000 THEN 'High-Performance'
WHEN total_sales BETWEEN 20000 AND 30000 THEN 'Mid Range'
WHEN total_sales < 2000 THEN 'Low-performance'
END AS product_segment,
(DATE_PART('year', AGE(current_DATE, last_order)) * 12)
+ DATE_PART('month', AGE(current_DATE, last_order)) AS recency,
CASE WHEN total_orders = 0 THEN 0
ELSE total_sales / total_orders
END AS average_order_revenue, -- (AOR)
CASE WHEN life_span = 0 THEN 0
ELSE ROUND(total_sales / life_span)
END AS average_monthly_revenue,
total_orders,
total_customers,
total_sales,
average_cost,
average_price
FROM
aggregation_product;
DROP VIEW IF EXISTS gold.report_customers;
DROP VIEW IF EXISTS gold.report_products;