-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_customer_segmentation.sql
More file actions
55 lines (36 loc) · 874 Bytes
/
1_customer_segmentation.sql
File metadata and controls
55 lines (36 loc) · 874 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
39
40
41
42
43
44
45
46
47
48
49
WITH customer_ltv AS (
SELECT
customerkey,
SUM (total_net_revenue) AS total_ltv,
full_name
FROM
cohort_analysis
GROUP BY
customerkey,
full_name
), customer_segment AS (
SELECT
PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY total_ltv) AS ltv_25th_percentile,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY total_ltv) AS ltv_75th_percentile
FROM customer_ltv
), segment_values AS (
SELECT
c.*,
CASE
WHEN c.total_ltv < cs.ltv_25th_percentile THEN '1 - LOW_value'
WHEN c.total_ltv <= cs.ltv_75th_percentile THEN '2 - low_value'
ELSE '3 - High_value'
END AS customer_segment
FROM
customer_ltv c,
customer_segment cs
)
SELECT
customer_segment,
SUM(total_ltv ),
count(customerkey) AS total_customer,
SUM (total_ltv) / count(customerkey) AS avg_ltv
FROM
segment_values
GROUP BY customer_segment
ORDER BY customer_segment DESC