-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveUsers.sql
93 lines (80 loc) · 2.15 KB
/
ActiveUsers.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
/*
https://datalemur.com/questions/user-retention
Active User Retention [Facebook SQL Interview Question]
The table below containing information on Facebook user actions.
Write a query to obtain the active user retention in July 2022.
Output the month (in numerical format 1, 2, 3) and the number of monthly active users (MAUs).
Hint: An active user is a user who has user action ("sign-in", "like", or "comment")
in the current month and last month.
FIRST SQL HARD PROBLEM.
This problem was hard. In fact, my current solution below takes 50 codes of line which seems too long.
I would likely need a better solution althought it gets the correct solution
*/
---More concise solution I did months later:
with facebook as(SELECT user_id, event_id,
EXTRACT(MONTH FROM event_date) as mth
FROM user_actions
WHERE event_type in ('sign-in', 'like', 'comment')
AND EXTRACT(YEAR FROM event_date)='2022'
AND EXTRACT(MONTH FROM event_date) IN ('6', '7') )
SELECT mth,
count(DISTINCT user_id) as monthly_active_users
FROM facebook
WHERE mth='7'
AND user_id IN
(SELECT user_id FROM facebook WHERE mth='6')
GROUP BY mth
;
---First Working Solution:
with facebook as (
SELECT user_id,
EXTRACT(MONTH FROM event_date) as m,
EXTRACT(YEAR FROM event_date) as y
FROM user_actions
),
cur_month as(
SELECT m as month, user_id
FROM facebook
WHERE m=7
AND y=2022
GROUP BY user_id, m, y
),
prev_month as (
SELECT m, user_id
FROM facebook
WHERE m=6
AND y=2022
GROUP BY user_id, m, y
),
active AS (SELECT month, user_id
FROM cur_month
JOIN prev_month
USING (user_id)
)
SELECT month, count(user_id) as monthly_active_users
FROM active
GROUP BY month
;
---FAILED ATTEMPT 1
---I had the right idea but definitely too complex has to be a better way
WITH facebook as (
SELECT EXTRACT(MONTH FROM event_date) as month,
EXTRACT(YEAR FROM event_date) as year,
user_id, event_id
FROM user_actions
WHERE event_type IN ('sign-in', 'like', 'comment')
),
active as (SELECT month, user_id
FROM facebook
WHERE year=2022
AND MONTH in (6, 7)
ORDER BY user_id
)
SELECT month, user_id
FROM active
WHERE (month,user_id) IN(SELECT month, user_id
FROM active
GROUP BY month, user_id
HAVING count(*)>0)
ORDER BY user_id
;