Table Schema
Inspect
Table
| Column | Type |
|---|---|
| user_id | integer |
| activity_date | date |
Classify users as new or returning based on activity.
Table Schema
| Column | Type |
|---|---|
| user_id | integer |
| activity_date | date |
Sample Data
| user_id | activity_date |
|---|---|
| 101 | 2024-01-01 |
| 101 | 2024-01-02 |
| 102 | 2024-01-01 |
| 103 | 2024-01-02 |
| 104 | 2024-01-03 |
| user_id | user_type |
|---|---|
| 101 | returning |
| 102 | new |
| 103 | new |
| 104 | new |
SQL Editor
| user_id | user_type |
|---|---|
| 101 | returning |
| 102 | new |
| 103 | new |
| 104 | new |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT user_id, CASE WHEN COUNT(DISTINCT activity_date) > 1 THEN 'returning' ELSE 'new' END AS user_type FROM activity GROUP BY user_id;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions