milestone 4/4 Top N per group & ties
Find the leading records within each group and handle ties.
Video Product Analytics · Move from watch events to daily engagement, rolling trends, and signup-cohort retention.
approach
Aggregate watched seconds per video, then rank videos within creator. ROW_NUMBER gives an exact N with a tie-breaker; DENSE_RANK includes ties and may return more than N. Make the tie policy explicit.
worked-example.sql
WITH totals AS( SELECT v.creator_id, v.id AS video_id, SUM(w.watch_seconds) AS watch_seconds FROM videos v JOIN watch_events w ON w.video_id=v.id GROUP BY v.id,v.creator_id ), ranked AS( SELECT *, ROW_NUMBER() OVER(PARTITION BY creator_id ORDER BY watch_seconds DESC,video_id) AS rn FROM totals ) SELECT creator_id,video_id,watch_seconds FROM ranked WHERE rn<=2;
data for this milestone
videos4 columns · 6 rows
| idinteger · key | creator_idinteger | titletext | duration_secondsinteger |
|---|---|---|---|
| 1 | 10 | SQL in ten minutes | 600 |
| 2 | 10 | Understanding joins | 900 |
| 3 | 11 | A day outdoors | 300 |
| 4 | 12 | Window functions | 720 |
| 5 | 12 | Build a project | 1200 |
showing 5 of 6 rows
watch_events5 columns · 26 rows
| idinteger · key | viewer_idinteger | video_idinteger | watched_attext | watch_secondsinteger |
|---|---|---|---|---|
| 1 | 1 | 1 | 2025-01-01 10:00:00 | 300 |
| 2 | 1 | 2 | 2025-01-01 11:00:00 | 450 |
| 3 | 2 | 1 | 2025-01-02 12:00:00 | 600 |
| 4 | 3 | 3 | 2025-01-02 13:00:00 | 120 |
| 5 | 1 | 4 | 2025-01-02 14:00:00 | 700 |
showing 5 of 26 rows