User Activity For The Past 30 Days II Problem
#1142
Description
LeetCode Problem 1142.
Table: Activity
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| session_id | int |
| activity_date | date |
| activity_type | enum |
+---------------+---------+
There is no primary key for this table, it may have duplicate rows.
The activity_type column is an ENUM of type ('open_session', 'end_session', 'scroll_down', 'send_message').
The table shows the user activities for a social media website.
Note that each session belongs to exactly one user.
Write an SQL query to find the average number of sessions per user for a period of 30 days ending 2019-07-27 inclusively, rounded to 2 decimal places. The sessions we want to count for a user are those with at least one activity in that time period.
The query result format is in the following example:
Activity table:
+---------+------------+---------------+---------------+
| user_id | session_id | activity_date | activity_type |
+---------+------------+---------------+---------------+
| 1 | 1 | 2019-07-20 | open_session |
| 1 | 1 | 2019-07-20 | scroll_down |
| 1 | 1 | 2019-07-20 | end_session |
| 2 | 4 | 2019-07-20 | open_session |
| 2 | 4 | 2019-07-21 | send_message |
| 2 | 4 | 2019-07-21 | end_session |
| 3 | 2 | 2019-07-21 | open_session |
| 3 | 2 | 2019-07-21 | send_message |
| 3 | 2 | 2019-07-21 | end_session |
| 3 | 5 | 2019-07-21 | open_session |
| 3 | 5 | 2019-07-21 | scroll_down |
| 3 | 5 | 2019-07-21 | end_session |
| 4 | 3 | 2019-06-25 | open_session |
| 4 | 3 | 2019-06-25 | end_session |
+---------+------------+---------------+---------------+
Result table:
+---------------------------+
| average_sessions_per_user |
+---------------------------+
| 1.33 |
+---------------------------+
User 1 and 2 each had 1 session in the past 30 days while user 3 had 2 sessions so the average is (1 + 1 + 2) / 3 = 1.33.
Solution
Approach: Filter the Period, Then Divide Distinct Counts
The average is (sessions in the period) / (users in the period), where both only count sessions with at least one activity in the period.
- The period. 30 days ending 2019-07-27 inclusively runs from
2019-06-28to2019-07-27.BETWEENincludes both ends. Make sure the filter has an upper bound too: activity after 2019-07-27 is outside the period. - Distinct counts. Each session has many activity rows, so count
DISTINCT session_id, and countDISTINCT user_idfor the users. Since each session belongs to exactly one user, every counted session belongs to a counted user. - No activity at all. Then both counts are 0 and
0 / 0isNULLin MySQL, soIFNULL(..., 0)returns 0 instead.
Round the result to 2 decimals.
The Pandas version filters activity_date with between (inclusive on both ends), divides the two nunique counts, and returns 0 when no user is active.
SELECT ROUND(
IFNULL(COUNT(DISTINCT session_id) / COUNT(DISTINCT user_id), 0),
2
) AS average_sessions_per_user
FROM Activity
-- 30 days ending 2019-07-27, both ends included.
WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27';