Game Play Analysis III

medium database sql window function

Problem

Return (player_id, event_date, games_played_so_far) where games_played_so_far is the cumulative games_played up to and including event_date.

Inputrows = [(1,'2016-03-01',5),(1,'2016-05-02',6),(1,'2017-06-25',1),(3,'2016-03-02',0),(3,'2018-07-03',5)]
Output[(1,'2016-03-01',5),(1,'2016-05-02',11),(1,'2017-06-25',12),(3,'2016-03-02',0),(3,'2018-07-03',5)]
Sort rows by (player, date) then accumulate per player.

SELECT player_id, event_date,
       SUM(games_played) OVER (PARTITION BY player_id ORDER BY event_date) AS games_played_so_far
FROM Activity;
Time: O(n log n) Space: O(players)