Table Schema
Inspect
Table
| Column | Type |
|---|---|
| user_id | integer |
| song_id | integer |
| duration_seconds | integer |
Calculate total listening time per user.
Table Schema
| Column | Type |
|---|---|
| user_id | integer |
| song_id | integer |
| duration_seconds | integer |
Sample Data
| user_id | song_id | duration_seconds |
|---|---|---|
| 101 | 1 | 200 |
| 101 | 2 | 180 |
| 102 | 1 | 220 |
| 103 | 3 | 300 |
| 101 | 3 | 250 |
| user_id | total_time |
|---|---|
| 101 | 630 |
| 102 | 220 |
| 103 | 300 |
SQL Editor
| user_id | total_time |
|---|---|
| 101 | 630 |
| 102 | 220 |
| 103 | 300 |
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, SUM(duration_seconds) AS total_time FROM streams 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