Top two artists each month by listening time
The task
The editorial team picks two featured artists per month for the home screen. For each calendar month, add up the seconds_played of all plays of each artist's tracks (a play belongs to the month of played_at). Only artists with at least one play in that month are ranked.
Return the top 2 artists of every month. If two artists have the same total, the one with the lower artist_id ranks higher, so every month returns exactly two rows when it has at least two artists with plays.
Return month (the first day of the month), artist_name, seconds_listened (the total) and position (1 or 2). Sort by month ascending, then position ascending.
Expected output
monthartist_nameseconds_listenedposition- Sorted by month ascending, then position ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Music streaming service · click a name to insert it
listeners180 rows in the sample
One row per listener account. Key: listener_id.
| int | Unique id. | |
| date | Day they joined. | |
| text | Two-letter country code. | |
| text | free, premium or family. |
artists30 rows in the sample
One row per artist. Key: artist_id.
| int | Unique id. | |
| text | Name. | |
| text | pop, rock, hip-hop, electronic, jazz or folk. |
tracks116 rows in the sample
One row per track. Key: track_id.
| int | Unique id. | |
| text | Track title. | |
| int | Main artist. | |
| int | Length. | |
| date | Release day. |
plays6,555 rows in the sample
One row per play. Key: play_id.
| bigint | Unique id. | |
| int | Who listened. | |
| int | What. | |
| timestamp | Start time (UTC). | |
| int | How long they listened. | |
| text | phone, desktop, speaker or car. |
Related questions
- How far each artist climbed or fell from April to Mayhard
- Listener quartiles by May listening timemedium
- Months each merchant ranked in the top three for spendmedium
- Best seller on each sales channel in the first quartermedium
- Favourite spending category per customer, counted by segmenthard
- First order value by acquisition channelmedium
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL