Quick Overview

Write a SQL query that returns, for every player and date, the total number of games that player has played up to and including that date. It checks running totals with window functions or self-joins, choosing the right partition, and ordering the output.

Compute Each Player's Running Total of Games Played by Date

Company: Amazon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `Activity` table has one row per player per day, with the device used and the number of games played that day. For each player and date, return `player_id`, `event_date`, and `games_played_so_far` (total games played by the player up to and including that date). Order by `player_id` and `event_date`. ```hint Which rows feed each total For one output row, list the input rows whose games belong in its total. Then think about how to get that set for every row without writing a separate query per date. ``` ### Input Table(s) `Activity` | Column | Type | Meaning | |---|---|---| | player_id | INT | Player | | device_id | INT | Device the player used that day | | event_date | DATE | Day of the activity | | games_played | INT | Games the player played that day | Key: `(player_id, event_date)` is the composite primary key. ### Output Contract - One row per row of `Activity`, with columns `player_id`, `event_date`, `games_played_so_far`. - `games_played_so_far` is the sum of `games_played` over all of that player's rows with `event_date` on or before the row's `event_date`. - Order by `player_id` ascending, then `event_date` ascending. ### Example `Activity` | player_id | device_id | event_date | games_played | |---|---|---|---| | 1 | 10 | 2024-01-05 | 3 | | 1 | 11 | 2024-01-02 | 2 | | 1 | 10 | 2024-01-09 | 0 | | 2 | 20 | 2024-01-03 | 4 | | 2 | 20 | 2024-01-04 | 1 | | 3 | 30 | 2024-01-06 | 7 | Expected result: | player_id | event_date | games_played_so_far | |---|---|---| | 1 | 2024-01-02 | 2 | | 1 | 2024-01-05 | 5 | | 1 | 2024-01-09 | 5 | | 2 | 2024-01-03 | 4 | | 2 | 2024-01-04 | 5 | | 3 | 2024-01-06 | 7 | Player 1's days in date order are 2024-01-02 (2 games), 2024-01-05 (3) and 2024-01-09 (0), so the running totals are 2, 5 and 5. The total carries across the switch from device 11 to device 10, and a zero-game day repeats the previous total. Player 2 reaches 4 and then 5, and player 3 has a single day with 7. ### Constraints and Clarifications - `(player_id, event_date)` is the composite primary key, so neither column is NULL and a player has at most one row per date. - `games_played >= 0` on every row. - `Activity` has at most 10,000 rows. - Rows are stored in no particular order. - `device_id` does not affect the total: games on different devices all count toward the same player.

Overview: Write a SQL query that returns, for every player and date, the total number of games that player has played up to and including that date. It checks running totals with window functions or self-joins, choosing the right partition, and ordering the output.

The `activity` table has one row per player per day, with the device used and the number of games played that day. Its columns are `player_id` (INTEGER, the player), `device_id` (INTEGER, the device the player used that day), `event_date` (DATE, the day of the activity) and `games_played` (INTEGER, the games the player played that day). `(player_id, event_date)` is the composite primary key, so neither column is NULL and a player has at most one row per date. `games_played >= 0` on every row, `activity` has at most 10,000 rows, and rows are stored in no particular order. For each player and date, return `player_id`, `event_date`, and `games_played_so_far` (total games played by the player up to and including that date). Return one row per row of `activity`. `games_played_so_far` is the sum of `games_played` over all of that player's rows with `event_date` on or before the row's `event_date`. `device_id` does not affect the total: games on different devices all count toward the same player. Order by `player_id` ascending, then `event_date` ascending.

Tables

activity(player_id INTEGER, device_id INTEGER, event_date DATE, games_played INTEGER)

Hints

  1. Which rows feed each total: for one output row, list the input rows whose games belong in its total. Then think about how to get that set for every row without writing a separate query per date.
AmazonData ScientistmediumData Manipulation (SQL/Python)

Running Total of Games Played per Player

The activity table has one row per player per day, with the device used and the number of games played that day. Its columns are player_id (INTEGER, the player), device_id (INTEGER, the device the player used that day), event_date (DATE, the day of the activity) and games_played (INTEGER, the games the player played that day). (player_id, event_date) is the composite primary key, so neither column is NULL and a player has at most one row per date. games_played >= 0 on every row, activity has at most 10,000 rows, and rows are stored in no particular order.

For each player and date, return player_id, event_date, and games_played_so_far (total games played by the player up to and including that date). Return one row per row of activity. games_played_so_far is the sum of games_played over all of that player's rows with event_date on or before the row's event_date. device_id does not affect the total: games on different devices all count toward the same player. Order by player_id ascending, then event_date ascending.

Expected Output

Tables

activity
activity
ColumnType
player_idINTEGER
device_idINTEGER
event_dateDATE
games_playedINTEGER

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions