Skip to content

Top two artists each month by listening time

MediumFull bankRanking and top NGroup and aggregateDates and time15 min2 hidden datasets

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.

intUnique id.
dateDay they joined.
textTwo-letter country code.
textfree, premium or family.
artists30 rows in the sample

One row per artist. Key: artist_id.

intUnique id.
textName.
textpop, rock, hip-hop, electronic, jazz or folk.
tracks116 rows in the sample

One row per track. Key: track_id.

intUnique id.
textTrack title.
intMain artist.
intLength.
dateRelease day.
plays6,555 rows in the sample

One row per play. Key: play_id.

bigintUnique id.
intWho listened.
intWhat.
timestampStart time (UTC).
intHow long they listened.
textphone, desktop, speaker or car.

Related questions

Loading the editor and PostgreSQL