{ "data": { "question": { "questionId": "3848", "questionFrontendId": "3497", "categoryTitle": "Database", "boundTopicId": 3630936, "title": "Analyze Subscription Conversion ", "titleSlug": "analyze-subscription-conversion", "content": "
Table: UserActivity
\n+------------------+---------+\n| Column Name | Type | \n+------------------+---------+\n| user_id | int |\n| activity_date | date |\n| activity_type | varchar |\n| activity_duration| int |\n+------------------+---------+\n(user_id, activity_date, activity_type) is the unique key for this table.\nactivity_type is one of ('free_trial', 'paid', 'cancelled').\nactivity_duration is the number of minutes the user spent on the platform that day.\nEach row represents a user's activity on a specific date.\n\n\n
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.
\n\n\n
Example:
\n\nInput:
\n\nUserActivity table:
\n\n\n+---------+---------------+---------------+-------------------+\n| user_id | activity_date | activity_type | activity_duration |\n+---------+---------------+---------------+-------------------+\n| 1 | 2023-01-01 | free_trial | 45 |\n| 1 | 2023-01-02 | free_trial | 30 |\n| 1 | 2023-01-05 | free_trial | 60 |\n| 1 | 2023-01-10 | paid | 75 |\n| 1 | 2023-01-12 | paid | 90 |\n| 1 | 2023-01-15 | paid | 65 |\n| 2 | 2023-02-01 | free_trial | 55 |\n| 2 | 2023-02-03 | free_trial | 25 |\n| 2 | 2023-02-07 | free_trial | 50 |\n| 2 | 2023-02-10 | cancelled | 0 |\n| 3 | 2023-03-05 | free_trial | 70 |\n| 3 | 2023-03-06 | free_trial | 60 |\n| 3 | 2023-03-08 | free_trial | 80 |\n| 3 | 2023-03-12 | paid | 50 |\n| 3 | 2023-03-15 | paid | 55 |\n| 3 | 2023-03-20 | paid | 85 |\n| 4 | 2023-04-01 | free_trial | 40 |\n| 4 | 2023-04-03 | free_trial | 35 |\n| 4 | 2023-04-05 | paid | 45 |\n| 4 | 2023-04-07 | cancelled | 0 |\n+---------+---------------+---------------+-------------------+\n\n\n
Output:
\n\n\n+---------+--------------------+-------------------+\n| user_id | trial_avg_duration | paid_avg_duration |\n+---------+--------------------+-------------------+\n| 1 | 45.00 | 76.67 |\n| 3 | 70.00 | 63.33 |\n| 4 | 37.50 | 45.00 |\n+---------+--------------------+-------------------+\n\n\n
Explanation:
\n\nThe 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.
\n表:UserActivity
\n+------------------+---------+\n| Column Name | Type | \n+------------------+---------+\n| user_id | int |\n| activity_date | date |\n| activity_type | varchar |\n| activity_duration| int |\n+------------------+---------+\n(user_id, activity_date, activity_type) 是这张表的唯一主键。\nactivity_type 是('free_trial', 'paid', 'cancelled')中的一个。\nactivity_duration 是用户当天在平台上花费的分钟数。\n每一行表示一个用户在特定日期的活动。\n\n\n
订阅服务想要分析用户行为模式。公司提供7天免费试用,试用结束后,用户可以选择订阅 付费计划 或 取消。编写解决方案:
\n\n2
位)2
位)返回结果表以 user_id
升序 排序。
结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\nUserActivity 表:
\n\n\n+---------+---------------+---------------+-------------------+\n| user_id | activity_date | activity_type | activity_duration |\n+---------+---------------+---------------+-------------------+\n| 1 | 2023-01-01 | free_trial | 45 |\n| 1 | 2023-01-02 | free_trial | 30 |\n| 1 | 2023-01-05 | free_trial | 60 |\n| 1 | 2023-01-10 | paid | 75 |\n| 1 | 2023-01-12 | paid | 90 |\n| 1 | 2023-01-15 | paid | 65 |\n| 2 | 2023-02-01 | free_trial | 55 |\n| 2 | 2023-02-03 | free_trial | 25 |\n| 2 | 2023-02-07 | free_trial | 50 |\n| 2 | 2023-02-10 | cancelled | 0 |\n| 3 | 2023-03-05 | free_trial | 70 |\n| 3 | 2023-03-06 | free_trial | 60 |\n| 3 | 2023-03-08 | free_trial | 80 |\n| 3 | 2023-03-12 | paid | 50 |\n| 3 | 2023-03-15 | paid | 55 |\n| 3 | 2023-03-20 | paid | 85 |\n| 4 | 2023-04-01 | free_trial | 40 |\n| 4 | 2023-04-03 | free_trial | 35 |\n| 4 | 2023-04-05 | paid | 45 |\n| 4 | 2023-04-07 | cancelled | 0 |\n+---------+---------------+---------------+-------------------+\n\n\n
输出:
\n\n\n+---------+--------------------+-------------------+\n| user_id | trial_avg_duration | paid_avg_duration |\n+---------+--------------------+-------------------+\n| 1 | 45.00 | 76.67 |\n| 3 | 70.00 | 63.33 |\n| 4 | 37.50 | 45.00 |\n+---------+--------------------+-------------------+\n\n\n
解释:
\n\n结果表仅包括从免费试用转为付费订阅的用户(用户 1,3 和 4),并且以 user_id 升序排序。
\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"UserActivity\":[\"user_id\",\"activity_date\",\"activity_type\",\"activity_duration\"]},\"rows\":{\"UserActivity\":[[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]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"