Median listen length, continuous and discrete
The task
The audio team reports a median listen length per device and wants to see how much the choice of percentile function matters.
A play counts as a listen when seconds_played is at least 30 (30 itself counts). Ignore shorter plays everywhere in this question.
For each device, return:
devicelistens: number of listensmedian_cont: the continuous median ofseconds_played(interpolates, so an even number of listens gives the average of the two middle values), rounded to 1 decimal placemedian_disc: the discrete median, which is an actualseconds_playedvalue (the first value whose cumulative share reaches 50 percent)
Sort by device.
Expected output
devicelistensmedian_contmedian_disc- Sorted by device ascending.
- Numbers rounded to 1 decimal place.
- 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
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL