Skip to content

Commit

Permalink
Update 0512. Game Play Analysis II.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
haibarawu authored Jun 9, 2020
1 parent f840e19 commit 0c830c6
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions Database/0512. Game Play Analysis II.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************
/****************************************************************************************************
512. Game Play Analysis II
Difficulty: Easy
Expand All @@ -15,7 +15,12 @@ Table: Activity
(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.
表的主键是 (player_id, event_date)。
这张表展示了一些游戏玩家在游戏平台上的行为活动。
每行数据记录了一名玩家在退出平台之前,当天使用同一台设备登录平台后打开的游戏的数目(可能是 0 个)。
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:
Expand All @@ -38,9 +43,17 @@ Result table:
| 2 | 3 |
| 3 | 1 |
+-----------+-----------+
********************************************************************************/
****************************************************************************************************/


/****************************************************************************************************
解题思路:
在 511 题中已经得到了用户第一次登录的时间 query01。
只需要 query01 与原始表 Activity 相关联,使 用户ID 和 登录时间 匹配,即可得到第一次登录平台使用的设备 device_id。
****************************************************************************************************/


--Method01:
SELECT DISTINCT player_id, device_id
FROM Activity
WHERE (player_id, event_date) IN (
Expand All @@ -49,3 +62,18 @@ WHERE (player_id, event_date) IN (
GROUP BY player_id
)


/****************************************************************************************************/


--Method02:
SELECT a.player_id, a.device_id
FROM Activity AS a,
( SELECT b.player_id, MIN(b.event_date) AS first_login
FROM Activity AS b
GROUP BY b.player_id
) AS f
WHERE a.player_id = f.player_id
AND a.event_date = f.first_login


0 comments on commit 0c830c6

Please sign in to comment.