Table Schema
Inspect
Table
| Column | Type |
|---|---|
| upload_id | integer |
| user_id | integer |
| upload_date | date |
Find users who uploaded photos on 3 consecutive days.
Table Schema
| Column | Type |
|---|---|
| upload_id | integer |
| user_id | integer |
| upload_date | date |
Sample Data
| upload_id | user_id | upload_date |
|---|---|---|
| 1 | 101 | 2024-01-01 |
| 2 | 101 | 2024-01-02 |
| 3 | 101 | 2024-01-03 |
| 4 | 102 | 2024-01-01 |
| 5 | 102 | 2024-01-03 |
| 6 | 103 | 2024-01-05 |
| 7 | 104 | 2024-01-06 |
| 8 | 104 | 2024-01-07 |
| user_id |
|---|
| 101 |
SQL Editor
| user_id |
|---|
| 101 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
WITH distinct_days AS (
SELECT DISTINCT user_id, upload_date
FROM photo_uploads
),
streaks AS (
SELECT
user_id,
upload_date - ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY upload_date)::int AS streak_group
FROM distinct_days
)
SELECT user_id
FROM streaks
GROUP BY user_id, streak_group
HAVING COUNT(*) >= 3;Explanation
Deduplicate user upload dates.
Normalize consecutive dates into the same group.
Return users with a 3 day upload streak.
Related Questions