Game Play Analysis III - Problem
Game Play Analysis III - Calculate Running Totals of Games Played
You're analyzing player activity data for a mobile gaming platform. Given a table of player login records, you need to calculate running totals of games played by each player over time.
Table: Activity
The primary key is
Goal: For each player and date, calculate the cumulative total of games played from their first login up to that date. This gives us insights into player engagement patterns over time.
Example: If Player 1 played 5 games on Jan 1st and 3 games on Jan 3rd, the result should show 5 games for Jan 1st and 8 games (5+3) for Jan 3rd.
You're analyzing player activity data for a mobile gaming platform. Given a table of player login records, you need to calculate running totals of games played by each player over time.
Table: Activity
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| player_id | int |
| device_id | int |
| event_date | date |
| games_played | int |
+--------------+---------+The primary key is
(player_id, event_date). Each row represents a player's gaming session on a specific date.Goal: For each player and date, calculate the cumulative total of games played from their first login up to that date. This gives us insights into player engagement patterns over time.
Example: If Player 1 played 5 games on Jan 1st and 3 games on Jan 3rd, the result should show 5 games for Jan 1st and 8 games (5+3) for Jan 3rd.
Input & Output
example_1.sql โ Basic Player Activity
$
Input:
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1 | 2 | 2016-03-01 | 5 |
| 1 | 2 | 2016-05-02 | 6 |
| 2 | 3 | 2017-06-25 | 1 |
| 3 | 1 | 2016-03-02 | 0 |
| 3 | 4 | 2018-07-03 | 5 |
+-----------+-----------+------------+--------------+
โบ
Output:
+-----------+------------+---------------------+
| player_id | event_date | games_played_so_far |
+-----------+------------+---------------------+
| 1 | 2016-03-01 | 5 |
| 1 | 2016-05-02 | 11 |
| 2 | 2017-06-25 | 1 |
| 3 | 2016-03-02 | 0 |
| 3 | 2018-07-03 | 5 |
+-----------+------------+---------------------+
๐ก Note:
Player 1: First login with 5 games (total: 5), second login with 6 more games (total: 5+6=11). Player 2: Only one session with 1 game. Player 3: First session with 0 games, second session adds 5 games for a total of 5.
example_2.sql โ Single Player Multiple Days
$
Input:
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1 | 1 | 2020-01-01 | 3 |
| 1 | 1 | 2020-01-02 | 2 |
| 1 | 1 | 2020-01-03 | 1 |
+-----------+-----------+------------+--------------+
โบ
Output:
+-----------+------------+---------------------+
| player_id | event_date | games_played_so_far |
+-----------+------------+---------------------+
| 1 | 2020-01-01 | 3 |
| 1 | 2020-01-02 | 5 |
| 1 | 2020-01-03 | 6 |
+-----------+------------+---------------------+
๐ก Note:
Player 1 plays consistently over 3 days. Running totals: Day 1: 3 games, Day 2: 3+2=5 games, Day 3: 3+2+1=6 games total.
example_3.sql โ Zero Games Edge Case
$
Input:
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1 | 1 | 2021-01-01 | 0 |
| 1 | 1 | 2021-01-02 | 0 |
| 1 | 1 | 2021-01-03 | 5 |
+-----------+-----------+------------+--------------+
โบ
Output:
+-----------+------------+---------------------+
| player_id | event_date | games_played_so_far |
+-----------+------------+---------------------+
| 1 | 2021-01-01 | 0 |
| 1 | 2021-01-02 | 0 |
| 1 | 2021-01-03 | 5 |
+-----------+------------+---------------------+
๐ก Note:
Player 1 logs in but doesn't play games for first two days (running total stays 0), then plays 5 games on third day (total becomes 5).
Visualization
Tap to expand
Understanding the Visualization
1
Group by Account (Player)
Separate transactions by account holder, like grouping bank statements by customer
2
Sort by Date
Arrange transactions chronologically to see the sequence of events
3
Calculate Running Balance
Add each transaction to the previous balance to get the new total
4
Display Statement
Show date, transaction amount, and running balance for each entry
Key Takeaway
๐ฏ Key Insight: Window functions eliminate the need for self-joins by efficiently calculating running sums in a single pass, making this optimal for large datasets just like modern banking systems process millions of transactions efficiently.
Time & Space Complexity
Time Complexity
O(n log n)
Single scan with sorting by player and date. Database optimizes the window function execution
โก Linearithmic
Space Complexity
O(n)
Space for sorting and intermediate window function calculations
โก Linearithmic Space
Constraints
- 1 โค Activity table rows โค 1000
- 1 โค player_id โค 1000
- 1 โค device_id โค 1000
- 0 โค games_played โค 1000
- event_date is in format 'YYYY-MM-DD'
- (player_id, event_date) is unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code