-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_queries.sql
More file actions
121 lines (98 loc) · 2.7 KB
/
Copy pathanalysis_queries.sql
File metadata and controls
121 lines (98 loc) · 2.7 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
-- BASIC EXPLORATION
-- Total Orders
SELECT COUNT(*) AS total_orders
FROM olist_orders_dataset;
-- Total Customers
SELECT COUNT(DISTINCT customer_id) AS total_customers
FROM olist_customers_dataset;
-- Total Revenue
SELECT SUM(payment_value) AS total_revenue
FROM olist_order_payments_dataset;
-- CORE DATASET (JOINED TABLE)
SELECT
o.order_id,
o.order_purchase_timestamp,
p.payment_value,
c.customer_state
FROM olist_orders_dataset o
JOIN olist_order_payments_dataset p
ON o.order_id = p.order_id
JOIN olist_customers_dataset c
ON o.customer_id = c.customer_id;
-- KPI ANALYSIS
-- Revenue Over Time
SELECT
DATE(order_purchase_timestamp) AS order_date,
SUM(payment_value) AS total_sales
FROM olist_orders_dataset o
JOIN olist_order_payments_dataset p
ON o.order_id = p.order_id
GROUP BY order_date
ORDER BY order_date;
-- Average Order Value
SELECT
SUM(payment_value) / COUNT(DISTINCT o.order_id) AS AOV
FROM olist_orders_dataset o
JOIN olist_order_payments_dataset p
ON o.order_id = p.order_id;
-- ADVANCED ANALYSIS
-- Monthly Revenue Growth
SELECT
strftime('%Y-%m', order_purchase_timestamp) AS month,
SUM(payment_value) AS revenue,
LAG(SUM(payment_value)) OVER (
ORDER BY strftime('%Y-%m', order_purchase_timestamp)
) AS prev_month
FROM olist_orders_dataset o
JOIN olist_order_payments_dataset p
ON o.order_id = p.order_id
GROUP BY month;
-- CUSTOMER ORDER FREQUENCY
SELECT
customer_id,
COUNT(order_id) AS order_count
FROM olist_orders_dataset
GROUP BY customer_id;
SELECT
order_count,
COUNT(*) AS customer_count
FROM (
SELECT
customer_id,
COUNT(order_id) AS order_count
FROM olist_orders_dataset
GROUP BY customer_id
) t
GROUP BY order_count
ORDER BY order_count;
-- CUSTOMER RETENTION
SELECT
CASE
WHEN order_count = 1 THEN 'One-time'
ELSE 'Repeat'
END AS customer_type,
COUNT(*) AS customer_count
FROM (
SELECT
customer_id,
COUNT(order_id) AS order_count
FROM olist_orders_dataset
GROUP BY customer_id
) t
GROUP BY customer_type;
WITH first_purchase AS (
SELECT
customer_id,
MIN(strftime('%Y-%m', order_purchase_timestamp)) AS cohort_month
FROM olist_orders_dataset
GROUP BY customer_id
)
SELECT
f.cohort_month,
strftime('%Y-%m', o.order_purchase_timestamp) AS order_month,
COUNT(DISTINCT o.customer_id) AS customers
FROM olist_orders_dataset o
JOIN first_purchase f
ON o.customer_id = f.customer_id
GROUP BY f.cohort_month, order_month
ORDER BY f.cohort_month, order_month;