All problems

What Share of Total Viewing Does Each Subscriber Represent?

hardSQLWindow Functions

StreamVerse's engagement team is worried about how concentrated its viewing is. If a handful of subscribers account for most of the minutes watched, then the headline "total hours streamed" figure is really a report on a few people's habits, and losing one of them would move the whole number. So they want each subscriber's slice of the pie rather than their raw minutes.

The difficulty is that every slice needs a figure computed from all the rows at once. A subscriber's own minutes come from their own sessions; the denominator is the minutes everybody watched, and it has to sit beside each subscriber's figure rather than being looked up separately and typed in.

watch_events — one row per viewing session. subscriber_id is the person who watched. watch_minutes is how long that session lasted.

id subscriber_id show_id watch_minutes watch_date
1 1 1 45 2023-05-01
2 1 5 60 2023-05-03
3 2 3 20 2023-05-02
4 3 2 55 2023-05-05
5 4 1 40 2023-05-06
6 4 4 70 2023-05-08
7 1 3 25 2023-05-10
8 3 5 50 2023-05-11
9 2 2 30 2023-05-12
10 5 4 65 2023-05-14

The table already exists in the database — there is nothing to create or load.

Task: Write a query returning two columns, subscriber_id and viewing_share, with one row per subscriber who has logged at least one session. viewing_share is that subscriber's total minutes divided by the total minutes across everybody — a fraction between 0 and 1, not a percentage, rounded to 4 decimal places. Every session counts, however short. A subscriber who has never watched anything produces no row rather than a row reading 0. Rows come back sorted by subscriber_id, smallest first.

Example output — shape only, on invented subscribers. The shares always come to 1 between them.

subscriber_id viewing_share
6 0.4375
8 0.25
9 0.3125

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…