-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretention_by_age
50 lines (22 loc) · 845 Bytes
/
retention_by_age
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 main_data as(
select *,CAST(RIGHT(week,length(week)-POSITION(' ' in week)) as int) as week_number
from table_1
)
,signup as (
select user_id,MIN(week_number) as signup_week
from main_data
group by 1
)
,final_data as (
select md.*, s.signup_week, week_number- s.signup_week as retention_week
from main_data md
left join signup s
on s.user_id = md.user_id
)
select age
,count(DISTINCT case when retention_week =0 then user_id else null end) as total_users
,count(distinct case when retention_week < 10 and retention_week >0 then user_id else null end) as "10_week_retention"
,count(distinct case when retention_week = 20 then user_id else null end) as "20_week_retention"
--,ARRAY_AGG(distinct case when retention_week < 10 then user_id else null end) as users
from final_data
GROUP by 1