-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query 2 - Annual Customer Activity Growth Analysis.sql
65 lines (65 loc) · 1.71 KB
/
Query 2 - Annual Customer Activity Growth Analysis.sql
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
--MAU (Monthly Active User) per year
WITH mau AS(
SELECT year, round(AVG(mau), 2) AS avg_mau
FROM(
SELECT date_part('year', o.order_purchase_timestamp) AS year,
date_part ('month', o.order_purchase_timestamp) AS month,
count(distinct c.customer_unique_id) AS mau
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
GROUP BY 1, 2
) subq
GROUP BY 1
ORDER BY 1 ASC
),
--new customer per year
new_customer AS(
SELECT date_part('year', first_order) AS year,
COUNT(DISTINCT customer_unique_id) AS total_new_customer
FROM(
SELECT c.customer_unique_id,
min(o.order_purchase_timestamp) AS first_order
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
GROUP BY 1
) subq
GROUP BY 1
ORDER BY 1 ASC
),
--repeat order customer per year
repeat AS(
SELECT year,
COUNT(customer) AS total_repeat_customer
FROM(
SELECT c.customer_unique_id,
COUNT(1) AS customer,
date_part('year', o.order_purchase_timestamp) AS year
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
GROUP BY 1, 3
HAVING COUNT (1) > 1
) subq
GROUP BY 1
ORDER BY 1 ASC
),
--average of order frequency per year
avg_freq AS(
SELECT year,
ROUND(AVG(total_order), 3) AS avg_total_order
FROM(
SELECT c.customer_unique_id,
date_part('year', o.order_purchase_timestamp) AS year,
COUNT(1) AS total_order
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id
GROUP BY 1, 2
) subsq
GROUP BY 1
ORDER BY 1 ASC
)
--combine the new metrics to be one table
SELECT m.year, m.avg_mau, nc.total_new_customer, r.total_repeat_customer, av.avg_total_order
FROM mau AS m
JOIN new_customer AS nc ON m.year = nc.year
JOIN repeat AS r ON m.year = r.year
JOIN avg_freq AS av ON m.year = av.year