[SQL_Q] 3497. Analyze Subscription Conversion

Hyunjun Kim·2025년 9월 1일
0

SQL

목록 보기
78/90

https://leetcode.com/problems/analyze-subscription-conversion/description/

문제

Table: UserActivity

+------------------+---------+
| Column Name      | Type    | 
+------------------+---------+
| user_id          | int     |
| activity_date    | date    |
| activity_type    | varchar |
| activity_duration| int     |
+------------------+---------+
(user_id, activity_date, activity_type) is the unique key for this table.
activity_type is one of ('free_trial', 'paid', 'cancelled').
activity_duration is the number of minutes the user spent on the platform that day.
Each row represents a user's activity on a specific date.
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:

Find users who converted from free trial to paid subscription
Calculate each user's average daily activity duration during their free trial period (rounded to 2 decimal places)
Calculate each user's average daily activity duration during their paid subscription period (rounded to 2 decimal places)
Return the result table ordered by user_id in ascending order.

The result format is in the following example.

 

Example:

Input:

UserActivity table:
+---------+---------------+---------------+-------------------+
| user_id | activity_date | activity_type | activity_duration |
+---------+---------------+---------------+-------------------+
| 1       | 2023-01-01    | free_trial    | 45                |
| 1       | 2023-01-02    | free_trial    | 30                |
| 1       | 2023-01-05    | free_trial    | 60                |
| 1       | 2023-01-10    | paid          | 75                |
| 1       | 2023-01-12    | paid          | 90                |
| 1       | 2023-01-15    | paid          | 65                |
| 2       | 2023-02-01    | free_trial    | 55                |
| 2       | 2023-02-03    | free_trial    | 25                |
| 2       | 2023-02-07    | free_trial    | 50                |
| 2       | 2023-02-10    | cancelled     | 0                 |
| 3       | 2023-03-05    | free_trial    | 70                |
| 3       | 2023-03-06    | free_trial    | 60                |
| 3       | 2023-03-08    | free_trial    | 80                |
| 3       | 2023-03-12    | paid          | 50                |
| 3       | 2023-03-15    | paid          | 55                |
| 3       | 2023-03-20    | paid          | 85                |
| 4       | 2023-04-01    | free_trial    | 40                |
| 4       | 2023-04-03    | free_trial    | 35                |
| 4       | 2023-04-05    | paid          | 45                |
| 4       | 2023-04-07    | cancelled     | 0                 |
+---------+---------------+---------------+-------------------+

Output:
+---------+--------------------+-------------------+
| user_id | trial_avg_duration | paid_avg_duration |
+---------+--------------------+-------------------+
| 1       | 45.00              | 76.67             |
| 3       | 70.00              | 63.33             |
| 4       | 37.50              | 45.00             |
+---------+--------------------+-------------------

내 쿼리

with a as (
    SELECT user_id, activity_type, round(avg(activity_duration),2) as  avg_duration
    FROM UserActivity
    GROUP BY user_id, activity_type
),
b as (
    SELECT user_id, avg_duration as trial_avg_duration
    FROM a 
    WHERE activity_type = "free_trial"
),
c as (
    SELECT user_id, avg_duration as paid_avg_duration
    FROM a
    WHERE activity_type = "paid"
)
SELECT b.user_id, trial_avg_duration, paid_avg_duration
FROM b JOIN c
on b.user_id = c.user_id

다른 사람 쿼리

SELECT *
FROM (
    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
    GROUP BY user_id 
) AS temp
WHERE paid_avg_duration IS NOT NULL

수정된 쿼리

SELECT *
FROM (
    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
    GROUP BY user_id
)as tmp
WHERE trial_avg_duration is NOT NULL 
and paid_avg_duration is NOT NULL
profile
Data Analytics Engineer 가 되

0개의 댓글