You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(user_id, activity_date, activity_type) is the unique key for this table.
16
+
activity_type is one of ('free_trial', 'paid', 'cancelled').
17
+
activity_duration is the number of minutes the user spent on the platform that day.
18
+
Each row represents a user's activity on a specific date.
19
+
A subscription service wants to analyze user behavior patterns. The company offers a 7-day free trial, after which users can subscribe to a paid plan or cancel. Write a solution to:
20
+
21
+
Find users who converted from free trial to paid subscription
22
+
Calculate each user's average daily activity duration during their free trial period (rounded to 2 decimal places)
23
+
Calculate each user's average daily activity duration during their paid subscription period (rounded to 2 decimal places)
24
+
Return the result table ordered by user_id in ascending order.
25
+
*/
26
+
27
+
WITH trial_durations AS (
28
+
SELECT
29
+
user_id,
30
+
AVG(activity_duration) AS trial_avg_duration
31
+
FROM UserActivity
32
+
WHERE activity_type ='free_trial'
33
+
GROUP BY user_id
34
+
),
35
+
36
+
paid_duration AS (
37
+
SELECT
38
+
user_id,
39
+
AVG(activity_duration) AS paid_avg_duration
40
+
FROM UserActivity
41
+
WHERE activity_type ='paid'
42
+
GROUP BY user_id
43
+
)
44
+
45
+
SELECT DISTINCT
46
+
u.user_id,
47
+
ROUND(t.trial_avg_duration, 2) AS trial_avg_duration,
48
+
ROUND(p.paid_avg_duration, 2) AS paid_avg_duration
0 commit comments