1141. User Activity for the Past 30 Days I
https://leetcode.com/problems/user-activity-for-the-past-30-days-i
Description
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 daily active user count for a period of 30 days ending 2019-07-27 inclusively. A user was active on some day if he/she made at least one activity on that day.
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 |
| 4 | 3 | 2019-06-25 | open\_session |
| 4 | 3 | 2019-06-25 | end\_session |
+---------+------------+---------------+---------------+
Result table:
+------------+--------------+
| day | active\_users |
+------------+--------------+
| 2019-07-20 | 2 |
| 2019-07-21 | 2 |
+------------+--------------+
Note that we do not care about days with zero active users.
ac
Last updated
Was this helpful?