Knowee
Questions
Features
Study Tools

Table: ActivityColumn NameTypeplayer_idintdevice_idintevent_datedategames_playedint(player_id, event_date) is the primary key of this table.This table shows the activity of players of some game. Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.Write an SQL query that reports for each player and date, how many games played so far by the player. That is, the total number of games played by the player until that date. Check the example for clarity.The query result format is in the following example:Activity table:player_iddevice_idevent_idgames_played122016-03-015122016-05-026132017-06-251312016-03-020342018-07-035Result table:player_idevent_dategames_played_so_far12016-03-01512016-05-021112017-06-251232016-03-02032018-07-035For the player with id 1, 5+6=11 games played by 2016-05-02, and 5+6+1=12 games played by 2017-06-25. For the player with id 3, 0+5=5 games played by 2018-07-03. Note that for each player we only care about the days when the player logged in.Optionsselect games_played_so_farfrom ( select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a, (select @player := -1, @games := 0) as tmp) as t;select player_id, event_date, games_played_so_farfrom ( select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a, (select @player := -1, @games := 0) as tmp) as t;select player_id, event_date, games_played_so_far select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a, (select @player := -1, @games := 0) as tmp) as t;select player_id, event_date, games_played_so_farfrom ( select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a,) as t;

Question

Table: ActivityColumn NameTypeplayer_idintdevice_idintevent_datedategames_playedint(player_id, event_date) is the primary key of this table.This table shows the activity of players of some game. Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.Write an SQL query that reports for each player and date, how many games played so far by the player. That is, the total number of games played by the player until that date. Check the example for clarity.The query result format is in the following example:Activity table:player_iddevice_idevent_idgames_played122016-03-015122016-05-026132017-06-251312016-03-020342018-07-035Result table:player_idevent_dategames_played_so_far12016-03-01512016-05-021112017-06-251232016-03-02032018-07-035For the player with id 1, 5+6=11 games played by 2016-05-02, and 5+6+1=12 games played by 2017-06-25. For the player with id 3, 0+5=5 games played by 2018-07-03. Note that for each player we only care about the days when the player logged in.Optionsselect games_played_so_farfrom ( select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a, (select @player := -1, @games := 0) as tmp) as t;select player_id, event_date, games_played_so_farfrom ( select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a, (select @player := -1, @games := 0) as tmp) as t;select player_id, event_date, games_played_so_far select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a, (select @player := -1, @games := 0) as tmp) as t;select player_id, event_date, games_played_so_farfrom ( select player_id, event_date, @games := if(player_id = @player, @games + games_played, games_played) as games_played_so_far, @player := player_id from (select * from Activity order by player_id, event_date) as a,) as t;

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct SQL query to solve this problem is:

SELECT player_id, event_date, SUM(games_played) OVER (PARTITION BY player_id ORDER BY event_date) as games_played_so_far
FROM Activity
ORDER BY player_id, event_date;

Here is the step by step explanation:

  1. SELECT player_id, event_date, SUM(games_played) OVER (PARTITION BY player_id ORDER BY event_date) as games_played_so_far: This line is selecting the player_id, event_date, and the cumulative sum of games_played for each player ordered by event_date. The SUM() OVER (PARTITION BY player_id ORDER BY event_date) part is a window function that calculates the cumulative sum of games_played for each player_id, ordered by event_date.

  2. FROM Activity: This line is specifying the table from which to select the data.

  3. ORDER BY player_id, event_date;: This line is ordering the result by player_id and event_date.

This query will give you the total number of games played by each player until each date they played.

This problem has been solved

Similar Questions

Game Play Analysis IITable: ActivityColumn NameTypeplayer_idintdevice_idintevent_datedategames_playedint(player_id, event_date) is the primary key of this table.This table shows the activity of players of some game. Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.Write a SQL query that reports the device that is first logged in for each player.The query result format is in the following example:Activity table:player_iddevice_idevent_idgames_played122016-03-015122016-05-026232017-06-251312016-03-020342018-07-035Result table:player_iddevice_id122331Optionsselect device_idfrom Activity where (player_id, event_date) in (select player_id, min(event_date)from Activity group by player_id);select player_id, device_idfrom Activitywhere (player_id, event_date) in (select player_id, min(event_date)group by player_id);select player_id, device_idfrom Activity (player_id, event_date) in ( select player_id, min(event_date) from Activity group by player_id);select player_id, device_idfrom Activitywhere (player_id, event_date) in ( select player_id, min(event_date) from Activity group by player_id);Finish

Alter the Sessions Table: Add a new column SessionDuration to record the duration of each gaming session. The data type INTERVAL DAY TO SECOND is used to store durations.

A database is being constructed to keep track of the teams and games of a sports league. A team has a number of players, not all of whom participate in each game. It is desired to keep track of the players participating in each game for each team, the positions they played in that game, and the result of the game. Choose your favorite sport (e.g., hockey, cricket, football, baseball, etc.).

A person wants to play online games. Get the number of times he needs to play the game. Permit him to play the game till the count is exceeded. Two types of games can be played namely ‘Game 1’ and ‘Game 2’. Create a base class ‘Game’. From class, ‘Game’, two classes ‘Game 1’ and ‘Game 2’ can be derived. Each time the player plays the game a unique integer id is created for each match. Get the details like name, phone number, score obtained for both games. Find the credit obtained and display it along with the report.

A person wants to play online games. Get the number of times he needs to play the game. Permit him to play the game till the count is exceeded. Two types of games can be played namely ‘Game 1’ and ‘Game 2’. Create a base class ‘Game’. From class, ‘Game’, two classes ‘Game 1’ and ‘Game 2’ can be derived. Each time the player plays the game a unique integer id is created for each match. Get the details like name, phone number, score obtained for both games. Find the credit obtained and display it along with the report.  For each match, a score is get as input from the player and credits are given based on the following criteria.ScoreCredits<1001100 to 1502150 to 2003200 to 2504Only for the ‘Game 2’ get the  mail id too. If the mail id is having any ‘(  ‘ symbol remove it and format the mail id and print the formatted mail id.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.