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 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 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);select device_idfrom Activity where (player_id, event_date) in (select player_id, min(event_date)from Activity group by player_id);
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 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 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);select device_idfrom Activity where (player_id, event_date) in (select player_id, min(event_date)from Activity group by player_id);
Solution
The correct SQL query to get the device that is first logged in for each player is:
SELECT player_id, device_id
FROM Activity
WHERE (player_id, event_date) IN (
SELECT player_id, MIN(event_date)
FROM Activity
GROUP BY player_id
);
This query works by first selecting the minimum event_date for each player_id from the Activity table. This subquery gives us the first login date for each player. Then, we select the player_id and device_id from the Activity table where the player_id and event_date match those of the first login dates. This gives us the device each player used to first log in.
Similar Questions
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;
You generate a report to show how many users are coming from various device types, like desktops and mobile phones, over the past 30 days.In this report, what is device type?A userA metricAn eventA dimension
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.
SELECT date(create_time) date, count(distinct device_id) FROM event_logs where event_id='home_page_view' GROUP BY date order by date desc union SELECT date(create_time) date, count(device_id) FROM event_logs where event_id='home_page_view' GROUP BY date order by date desc
SELECT DISTINCT el.device_id, DATE(el.create_time) date, CASE WHEN el.platform IN ('') THEN 'null' ELSE 'unknown' END AS platform_div, CASE WHEN el.event_id='home_page_view' THEN 1 ELSE 0,caseWHEN ud.country ='' THEN 'unknown' ELSE 'developing_country' END AS country_div FROM event_logs el JOIN user_devices ud ON el.device_id = ud.device_id我要如何在查询结果中加入每个device_id使用过的次数
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.