
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:
2 decimal places)2 decimal places)Return the result table ordered by user_id in ascending order.
The result format is in the following example.
CASE 절로 각 activity_type에 맞는 값들의 평균을 구해준다.activity_type이 free_trial과 paid중 하나에 속하면서, 현재 activity_type이 paid인 user_id를 찾는다.free_trial이기 때문에 가능user_id별로 GROUP BY 한 후, 정렬SELECT
user_id,
ROUND(AVG(
CASE
WHEN activity_type = 'free_trial' THEN activity_duration
END)
, 2) AS trial_avg_duration,
ROUND(AVG(
CASE
WHEN activity_type = 'paid' THEN activity_duration
END)
, 2) AS paid_avg_duration
FROM UserActivity
WHERE activity_type IN ('free_trial', 'paid')
AND user_id IN (
SELECT user_id
FROM UserActivity
WHERE activity_type = 'paid'
)
GROUP BY user_id
ORDER BY user_id;