-
Notifications
You must be signed in to change notification settings - Fork 0
/
churn_rate.sql.txt
240 lines (228 loc) · 5.19 KB
/
churn_rate.sql.txt
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
SELECT *
FROM subscriptions
LIMIT 100
;
--discover the different segments of users
SELECT DISTINCT segment
FROM subscriptions
;
-- calculate the range of months
--check 1 enough data during first month for total subscriptions
--check 2 enough data during last month for cancellations by end of month
SELECT MIN(subscription_start), MAX(subscription_start),
MIN(subscription_end), MAX(subscription_end)
FROM subscriptions
;
--count number of users in each section
SELECT segment, COUNT(*) as Total_users
FROM subscriptions
WHERE segment = 30
;
SELECT segment, COUNT(*) as Total_users
FROM subscriptions
WHERE segment = 87
;
--overall churn rate at Codeflix
SELECT ROUND(1.0 *
( SELECT COUNT(*)
FROM subscriptions
WHERE subscription_start < '2017-01-01'
AND (subscription_end
BETWEEN '2017-01-01'
AND '2017-03-31'
)) / (
SELECT COUNT(*)
FROM subscriptions
WHERE subscription_start < '2017-01-01'
AND ((subscription_end >= '2017-01-01')
OR (subscription_end IS NULL)
)),2) AS Overall_Churn_Rate
;
--calculate churn rate each month
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,
CASE
WHEN subscription_start < first_day
AND
(subscription_end >= first_day
OR subscription_end IS NULL)
THEN 1
ELSE 0
END as is_active
,
CASE
WHEN subscription_end
BETWEEN first_day
AND last_day
THEN 1
ELSE 0
END as is_canceled
FROM cross_join)
,
status_aggregate AS (
SELECT
month,
SUM(is_active) as sum_active,
SUM(is_canceled) as sum_canceled
FROM status
GROUP BY 1)
SELECT
month,
ROUND(1.0 * sum_canceled / sum_active,2)
as Churn_Rate_Each_Month
FROM status_aggregate
;
--calculate churn by segment each month
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,
CASE
WHEN (segment = 87)
AND
(subscription_start < first_day)
AND
(subscription_end >= first_day
OR subscription_end IS NULL)
THEN 1
ELSE 0
END as is_active_87
,
CASE
WHEN (segment = 30)
AND
(subscription_start < first_day)
AND (subscription_end >= first_day
OR subscription_end IS NULL)
THEN 1
ELSE 0
END as is_active_30
,
CASE
WHEN segment = 87
AND
(subscription_end
BETWEEN first_day AND last_day)
THEN 1
ELSE 0
END as is_canceled_87
,
CASE
WHEN segment = 30
AND
(subscription_end
BETWEEN first_day AND last_day)
THEN 1
ELSE 0
END as is_canceled_30
FROM cross_join)
,
status_aggregate AS (
SELECT
month,
SUM(is_active_87) as sum_active_87,
SUM(is_active_30) as sum_active_30,
SUM(is_canceled_87) as sum_canceled_87,
SUM(is_canceled_30) as sum_canceled_30
FROM status
GROUP BY month)
SELECT
month,
ROUND(1.0 * sum_canceled_87 / sum_active_87,2)
as churn_rate_87,
ROUND(1.0 * sum_canceled_30 / sum_active_30,2)
as churn_rate_30
FROM status_aggregate
;
--calculate churn rate for more segments
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,
CASE
WHEN subscription_start < first_day
AND
(subscription_end >= first_day
OR subscription_end IS NULL)
THEN 1
ELSE 0
END as is_active
,
CASE
WHEN subscription_end
BETWEEN first_day
AND last_day
THEN 1
ELSE 0
END as is_canceled, segment
FROM cross_join)
,
status_aggregate AS (
SELECT month,
SUM(is_active) as active,
SUM(is_canceled) as canceled,
segment
FROM status
GROUP BY 4, 1)
SELECT
month,
segment,
ROUND(1.0 * canceled/active,2) as churn_rate
FROM status_aggregate
;