You are given a table Activity that tracks game playing activity for multiple players across different dates.
Table Structure:
player_id(int): Unique identifier for each playerdevice_id(int): Device used by the playerevent_date(date): Date when the player logged ingames_played(int): Number of games played on that date
Task: Write a SQL query to calculate the cumulative total of games played by each player up to each date. For every player and date combination, show how many games that player has played in total from their first login until that specific date.
The result should include player_id, event_date, and games_played_so_far (the running total).
Table Schema
| Column Name | Type | Description |
|---|---|---|
player_id
PK
|
int | Unique identifier for each player |
device_id
|
int | Device ID used by the player |
event_date
PK
|
date | Date when the player logged in and played games |
games_played
|
int | Number of games played on that specific date |
Input & Output
| player_id | device_id | event_date | games_played |
|---|---|---|---|
| 1 | 2 | 2016-03-01 | 5 |
| 1 | 2 | 2016-03-02 | 6 |
| 2 | 3 | 2017-06-25 | 1 |
| 3 | 1 | 2016-03-02 | 0 |
| 3 | 4 | 2018-07-03 | 5 |
| player_id | event_date | games_played_so_far |
|---|---|---|
| 1 | 2016-03-01 | 5 |
| 1 | 2016-03-02 | 11 |
| 2 | 2017-06-25 | 1 |
| 3 | 2016-03-02 | 0 |
| 3 | 2018-07-03 | 5 |
For player 1: First day played 5 games (total=5), second day played 6 more games (total=5+6=11). For player 2: Only played once with 1 game (total=1). For player 3: First day played 0 games (total=0), later played 5 games (total=0+5=5).
| player_id | device_id | event_date | games_played |
|---|---|---|---|
| 1 | 1 | 2020-01-01 | 3 |
| 1 | 1 | 2020-01-02 | 2 |
| 1 | 2 | 2020-01-03 | 4 |
| player_id | event_date | games_played_so_far |
|---|---|---|
| 1 | 2020-01-01 | 3 |
| 1 | 2020-01-02 | 5 |
| 1 | 2020-01-03 | 9 |
Player 1's cumulative game count: Day 1 = 3, Day 2 = 3+2 = 5, Day 3 = 5+4 = 9. The running total increases with each gaming session.
Constraints
-
1 ≤ player_id ≤ 10^4 -
1 ≤ device_id ≤ 10^4 -
games_played ≥ 0 -
event_dateis a valid date