Skip to content

Commit 6383d2e

Browse files
committed
task: #3497
1 parent dd56d01 commit 6383d2e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
174174
- [1907. Count Salary Categories](./leetcode/medium/1907.%20Count%20Salary%20Categories.sql)
175175
- [1934. Confirmation Rate](./leetcode/medium/1934.%20Confirmation%20Rate.sql)
176176
- [3475. DNA Pattern Recognition](./leetcode/medium/3475.%20DNA%20Pattern%20Recognition.sql)
177+
- [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql)
177178
3. [Hard](./leetcode/hard/)
178179
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
179180
- [3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Question 3497. Analyze Subscription Conversion
3+
Link: https://leetcode.com/problems/analyze-subscription-conversion/description/?envType=problem-list-v2&envId=database
4+
5+
Table: UserActivity
6+
7+
+------------------+---------+
8+
| Column Name | Type |
9+
+------------------+---------+
10+
| user_id | int |
11+
| activity_date | date |
12+
| activity_type | varchar |
13+
| activity_duration| int |
14+
+------------------+---------+
15+
(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
49+
FROM UserActivity AS u
50+
INNER JOIN
51+
trial_durations AS t
52+
ON u.user_id = t.user_id
53+
INNER JOIN
54+
paid_duration AS p
55+
ON u.user_id = p.user_id
56+

0 commit comments

Comments
 (0)