Given a Traffic table that tracks user activities, write a SQL solution to report the number of users that logged in for the first time on each date within at most 90 days from today (2019-06-30).
The table contains columns:
user_id(int): User identifieractivity(enum): Activity type ('login', 'logout', 'jobs', 'groups', 'homepage')activity_date(date): Date of the activity
Key Requirements:
- Only consider dates within 90 days from 2019-06-30 (i.e., from 2019-04-01 to 2019-06-30)
- Find users who logged in for the first time on each date
- Count distinct users per date
- The table may contain duplicate rows
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
|
int | User identifier |
activity
|
enum | Activity type: 'login', 'logout', 'jobs', 'groups', 'homepage' |
activity_date
|
date | Date when the activity occurred |
Input & Output
| user_id | activity | activity_date |
|---|---|---|
| 1 | login | 2019-05-01 |
| 1 | homepage | 2019-05-01 |
| 1 | logout | 2019-05-01 |
| 2 | login | 2019-06-21 |
| 2 | logout | 2019-06-21 |
| 3 | login | 2019-05-01 |
| 3 | logout | 2019-05-01 |
| 3 | login | 2019-06-21 |
| 4 | login | 2019-06-21 |
| 4 | login | 2019-05-01 |
| login_date | user_count |
|---|---|
| 2019-05-01 | 3 |
| 2019-06-21 | 1 |
User 1's first login: 2019-05-01, User 2's first login: 2019-06-21, User 3's first login: 2019-05-01 (not 2019-06-21), User 4's first login: 2019-05-01 (not 2019-06-21). So on 2019-05-01, we have 3 new users (1,3,4), and on 2019-06-21, we have 1 new user (2).
| user_id | activity | activity_date |
|---|---|---|
| 1 | login | 2019-03-01 |
| 1 | login | 2019-05-01 |
| 2 | login | 2019-06-30 |
| login_date | user_count |
|---|---|
| 2019-06-30 | 1 |
User 1's first login was 2019-03-01, which is outside the 90-day window (before 2019-04-01), so user 1 is not counted. User 2's first login 2019-06-30 is within range, so we count 1 new user on that date.
| user_id | activity | activity_date |
|---|---|---|
| 1 | homepage | 2019-05-01 |
| 2 | logout | 2019-06-21 |
| login_date | user_count |
|---|
No login activities found in the data, so the result is empty. Only 'homepage' and 'logout' activities are present, which don't count as first-time logins.
Constraints
-
1 ≤ user_id ≤ 100000 -
activityis one of('login', 'logout', 'jobs', 'groups', 'homepage') -
activity_dateis between2019-01-01and2019-06-30 -
Consider only dates within 90 days from
2019-06-30