-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.sql
176 lines (152 loc) · 3.96 KB
/
code.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
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
-- SELECT THE FIRST 100 rows from the table
SELECT *
FROM subscriptions
LIMIT 100;
-- Based on the code there appears to only be two segments (87 or 30)
-- SELECT THE NUMBER OF DISTINCT SEGMENTS, count the number of users in each segment
SELECT DISTINCT segment, COUNT(*) as 'Number of customers'
FROM subscriptions
GROUP BY segment;
-- How we would find the available months for churn
SELECT MIN(subscription_start),MAX(subscription_start)
FROM subscriptions;
-- CREATE A temporary table for MONTHS and Utilize a cross join along with case statement to convert active subs into flag column 1 or 0
WITH months AS
(SELECT
'2017-01-01' as first_day,
'2017-01-31' as last_day
UNION
SELECT
'2017-02-01' as first_day,
'2017-02-28' as last_day
UNION
SELECT
'2017-03-01' as first_day,
'2017-03-31' as last_day
),
cross_join AS
(SELECT *
FROM subscriptions
CROSS JOIN months),
status AS
(SELECT id, first_day as month,
-- The is total active segment
CASE
WHEN (subscription_start < first_day)
AND (
subscription_end > first_day
OR subscription_end IS NULL
)
THEN 1
ELSE 0
END as is_active,
-- Here we'll create out cancelled segments
CASE
WHEN (subscription_end BETWEEN first_day AND last_day)
THEN 1
ELSE 0
END as is_canceled
FROM cross_join),
-- Lets compute our Aggregate numbers for both segments
status_aggregate AS
(SELECT
month,
SUM(is_active) as sum_active,
SUM(is_canceled) as sum_canceled
FROM status
GROUP BY month)
-- Compute the Churn Rates and round the rates
SELECT month,
ROUND(1.0*sum_canceled/sum_active,2) as 'overall churn_rate'
FROM status_aggregate;
-- CREATE A temporary table for MONTHS
WITH months AS
(SELECT
'2017-01-01' as first_day,
'2017-01-31' as last_day
UNION
SELECT
'2017-02-01' as first_day,
'2017-02-28' as last_day
UNION
SELECT
'2017-03-01' as first_day,
'2017-03-31' as last_day
),
-- Now we can cross Join the Months table with the subscriptions table
cross_join AS
(SELECT *
FROM subscriptions
CROSS JOIN months),
-- Now we'll Create the temporary status table for the two customer Segments
status AS
(SELECT id, first_day as month,
-- These are the total active segments
CASE
WHEN (subscription_start < first_day)
AND (
subscription_end > first_day
OR subscription_end IS NULL
)
THEN 1
ELSE 0
END as is_active,
-- These are the cancelled segments
CASE
WHEN (subscription_end BETWEEN first_day AND last_day)
THEN 1
ELSE 0
END as is_canceled,
-- Derive the active 87 segment
CASE
WHEN (subscription_start < first_day)
AND (
subscription_end > first_day
OR subscription_end IS NULL
)
AND segment = 87
THEN 1
ELSE 0
END as is_active_87,
-- Derive the cancelled 87 segment
CASE
WHEN (subscription_end BETWEEN first_day AND last_day) AND ( segment= 87)
THEN 1
ELSE 0
END as is_canceled_87,
-- Derive the active 30 segments
CASE
WHEN (subscription_start < first_day)
AND (
subscription_end > first_day
OR subscription_end IS NULL
)
AND segment = 30
THEN 1
ELSE 0
END as is_active_30,
-- And the cancelled 30 segments
CASE
WHEN (subscription_end BETWEEN first_day AND last_day) AND ( segment= 30)
THEN 1
ELSE 0
END as is_canceled_30
FROM cross_join),
-- Now we'll generate the Aggregate numbers for both segments
status_aggregate AS
(SELECT
month,
SUM(is_active) as sum_active,
SUM(is_active_30) as sum_active_30,
SUM(is_active_87) as sum_active_87,
SUM(is_canceled) as sum_canceled,
SUM(is_canceled_30) as sum_canceled_30,
SUM(is_canceled_87) as sum_canceled_87
FROM status
GROUP BY month)
-- Compute the Churn Rates & Round
SELECT month,
ROUND(1.0*sum_canceled/sum_active,2) as total_churn_rate,
ROUND(1.0*sum_canceled_30/sum_active_30,2) as churn_rate_30,
ROUND(1.0*sum_canceled_87/sum_active_87,2) as churn_rate_87
FROM status_aggregate;