|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3497\. Analyze Subscription Conversion |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Table: `UserActivity` |
| 9 | + |
| 10 | + +------------------+---------+ |
| 11 | + | Column Name | Type | |
| 12 | + +------------------+---------+ |
| 13 | + | user_id | int | |
| 14 | + | activity_date | date | |
| 15 | + | activity_type | varchar | |
| 16 | + | activity_duration| int | |
| 17 | + +------------------+---------+ |
| 18 | + (user_id, activity_date, activity_type) is the unique key for this table. activity_type is one of ('free_trial', 'paid', 'cancelled'). |
| 19 | + activity_duration is the number of minutes the user spent on the platform that day. |
| 20 | + Each row represents a user's activity on a specific date. |
| 21 | + |
| 22 | +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: |
| 23 | + |
| 24 | +1. Find users who converted from free trial to paid subscription |
| 25 | +2. Calculate each user's **average daily activity duration** during their **free trial** period (rounded to `2` decimal places) |
| 26 | +3. Calculate each user's **average daily activity duration** during their **paid** subscription period (rounded to `2` decimal places) |
| 27 | + |
| 28 | +Return _the result table ordered by_ `user_id` _in **ascending** order_. |
| 29 | + |
| 30 | +The result format is in the following example. |
| 31 | + |
| 32 | +**Example:** |
| 33 | + |
| 34 | +**Input:** |
| 35 | + |
| 36 | +UserActivity table: |
| 37 | + |
| 38 | +| user_id | activity_date | activity_type | activity_duration | |
| 39 | +|---------|---------------|---------------|-------------------| |
| 40 | +| 1 | 2023-01-01 | free_trial | 45 | |
| 41 | +| 1 | 2023-01-02 | free_trial | 30 | |
| 42 | +| 1 | 2023-01-05 | free_trial | 60 | |
| 43 | +| 1 | 2023-01-10 | paid | 75 | |
| 44 | +| 1 | 2023-01-12 | paid | 90 | |
| 45 | +| 1 | 2023-01-15 | paid | 65 | |
| 46 | +| 2 | 2023-02-01 | free_trial | 55 | |
| 47 | +| 2 | 2023-02-03 | free_trial | 25 | |
| 48 | +| 2 | 2023-02-07 | free_trial | 50 | |
| 49 | +| 2 | 2023-02-10 | cancelled | 0 | |
| 50 | +| 3 | 2023-03-05 | free_trial | 70 | |
| 51 | +| 3 | 2023-03-06 | free_trial | 60 | |
| 52 | +| 3 | 2023-03-08 | free_trial | 80 | |
| 53 | +| 3 | 2023-03-12 | paid | 50 | |
| 54 | +| 3 | 2023-03-15 | paid | 55 | |
| 55 | +| 3 | 2023-03-20 | paid | 85 | |
| 56 | +| 4 | 2023-04-01 | free_trial | 40 | |
| 57 | +| 4 | 2023-04-03 | free_trial | 35 | |
| 58 | +| 4 | 2023-04-05 | paid | 45 | |
| 59 | +| 4 | 2023-04-07 | cancelled | 0 | |
| 60 | + |
| 61 | +**Output:** |
| 62 | + |
| 63 | +| user_id | trial_avg_duration | paid_avg_duration | |
| 64 | +|---------|--------------------|-------------------| |
| 65 | +| 1 | 45.00 | 76.67 | |
| 66 | +| 3 | 70.00 | 63.33 | |
| 67 | +| 4 | 37.50 | 45.00 | |
| 68 | + |
| 69 | +**Explanation:** |
| 70 | + |
| 71 | +* **User 1:** |
| 72 | + * Had 3 days of free trial with durations of 45, 30, and 60 minutes. |
| 73 | + * Average trial duration: (45 + 30 + 60) / 3 = 45.00 minutes. |
| 74 | + * Had 3 days of paid subscription with durations of 75, 90, and 65 minutes. |
| 75 | + * Average paid duration: (75 + 90 + 65) / 3 = 76.67 minutes. |
| 76 | +* **User 2:** |
| 77 | + * Had 3 days of free trial with durations of 55, 25, and 50 minutes. |
| 78 | + * Average trial duration: (55 + 25 + 50) / 3 = 43.33 minutes. |
| 79 | + * Did not convert to a paid subscription (only had free\_trial and cancelled activities). |
| 80 | + * Not included in the output because they didn't convert to paid. |
| 81 | +* **User 3:** |
| 82 | + * Had 3 days of free trial with durations of 70, 60, and 80 minutes. |
| 83 | + * Average trial duration: (70 + 60 + 80) / 3 = 70.00 minutes. |
| 84 | + * Had 3 days of paid subscription with durations of 50, 55, and 85 minutes. |
| 85 | + * Average paid duration: (50 + 55 + 85) / 3 = 63.33 minutes. |
| 86 | +* **User 4:** |
| 87 | + * Had 2 days of free trial with durations of 40 and 35 minutes. |
| 88 | + * Average trial duration: (40 + 35) / 2 = 37.50 minutes. |
| 89 | + * Had 1 day of paid subscription with duration of 45 minutes before cancelling. |
| 90 | + * Average paid duration: 45.00 minutes. |
| 91 | + |
| 92 | +The result table only includes users who converted from free trial to paid subscription (users 1, 3, and 4), and is ordered by user\_id in ascending order. |
| 93 | + |
| 94 | +## Solution |
| 95 | + |
| 96 | +```sql |
| 97 | +# Write your MySQL query statement below |
| 98 | +SELECT |
| 99 | + ft.user_id, |
| 100 | + ROUND(ft.avg_trial, 2) AS trial_avg_duration, |
| 101 | + ROUND(pt.avg_paid, 2) AS paid_avg_duration |
| 102 | +FROM |
| 103 | + (SELECT user_id, AVG(activity_duration) AS avg_trial |
| 104 | + FROM UserActivity |
| 105 | + WHERE activity_type = 'free_trial' |
| 106 | + GROUP BY user_id) ft |
| 107 | +JOIN |
| 108 | + (SELECT user_id, AVG(activity_duration) AS avg_paid |
| 109 | + FROM UserActivity |
| 110 | + WHERE activity_type = 'paid' |
| 111 | + GROUP BY user_id) pt |
| 112 | +ON ft.user_id = pt.user_id |
| 113 | +ORDER BY ft.user_id ASC; |
| 114 | +``` |
0 commit comments