The same play (same profile_id, title_id and started_at) can be reported by several clients. `source` is messy text: compare it as lower(trim(source)). Keep ONE row per play using priority 'tv' first, then 'web', then 'mobile'; within the same source keep the larger `watched_min` (NULL counts as 0), then the lower `log_id`. From the kept rows return per profile: `profile_id`, `plays`, `total_watch_min` (NULL as 0), `tv_share_pct` = ROUND(100.0 * plays whose kept source is 'tv' / plays, 1). Order by `profile_id`.
| Column | Type |
|---|---|
| log_id | INTEGER |
| profile_id | INTEGER |
| title_id | INTEGER |
| started_at | TIMESTAMP |
| source | TEXT |
| watched_min | INTEGER |
| log_id | profile_id | title_id | started_at | source | watched_min |
|---|---|---|---|---|---|
| 1 | 1 | 900 | 2026-11-01 20:00:00 | mobile | 50 |
| 2 | 1 | 900 | 2026-11-01 20:00:00 | tv | 45 |
| 3 | 1 | 901 | 2026-11-01 21:00:00 | web | 30 |
| 4 | 1 | 901 | 2026-11-01 21:00:00 | mobile | 31 |
| 5 | 2 | 900 | 2026-11-02 08:00:00 | mobile | 20 |
| 6 | 2 | 902 | 2026-11-02 09:00:00 | tv | 60 |
| 7 | 2 | 902 | 2026-11-02 09:00:00 | tv | 58 |
| profile_id | plays | total_watch_min | tv_share_pct |
|---|---|---|---|
| 1 | 2 | 75 | 50 |
| 2 | 2 | 80 | 50 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Riders Taking a Second Trip Within 7 Days · Category Breadcrumbs for Leaf Nodes ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.