From dc267ca3be7eb31bcc393794b60671821d907531 Mon Sep 17 00:00:00 2001 From: haibarawu Date: Tue, 9 Jun 2020 12:41:51 -0700 Subject: [PATCH] Update and rename 0550. Game Play Analysis IV.sql to 0550.(Medium) Game Play Analysis IV.sql --- ...> 0550.(Medium) Game Play Analysis IV.sql} | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) rename Database/{0550. Game Play Analysis IV.sql => 0550.(Medium) Game Play Analysis IV.sql} (54%) diff --git a/Database/0550. Game Play Analysis IV.sql b/Database/0550.(Medium) Game Play Analysis IV.sql similarity index 54% rename from Database/0550. Game Play Analysis IV.sql rename to Database/0550.(Medium) Game Play Analysis IV.sql index 47446e1..a267ac5 100644 --- a/Database/0550. Game Play Analysis IV.sql +++ b/Database/0550.(Medium) Game Play Analysis IV.sql @@ -1,4 +1,9 @@ -/******************************************************************************** +/**************************************************************************************************** +550. Game Play Analysis IV +550. 游戏玩法分析 IV + +Difficulty: Medium + Table: Activity +--------------+---------+ | Column Name | Type | @@ -11,9 +16,15 @@ 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 an SQL query that reports the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places. In other words, you need to count the number of players that logged in for at least two consecutive days starting from their first login date, then divide that number by the total number of players. +编写一个 SQL 查询,报告在首次登录的第二天再次登录的玩家的分数,四舍五入到小数点后两位。 +换句话说,您需要计算从首次登录日期开始至少连续两天登录的玩家的数量,然后除以玩家总数。 The query result format is in the following example: @@ -35,18 +46,28 @@ Result table: | 0.33 | +-----------+ Only the player with id 1 logged back in after the first day he had logged in so the answer is 1/3 = 0.33 -********************************************************************************/ +只有 ID 为 1 的玩家在第一天登录后才重新登录,所以答案是 1/3 = 0.33 +****************************************************************************************************/ + + +/**************************************************************************************************** +解题思路: +在 511 题中已经得到了用户第一次登录的时间 query01。 +根据题意我们要去查询用户在第一次登录的第二天的登录情况和第二天的游戏分数。 +只需要 query01 与原始表 Activity 相关联,使 <用户ID>相同 和 <登录时间>相差一天 匹配,得到要查询的目标ID。 +再套用一层 COUNT() 来分别统计 第二天也登录了的人数 / 总人数,两者相除,去小数点后两位来得到结果。 +****************************************************************************************************/ SELECT ROUND(COUNT(B.player_id) / COUNT(A.player_id) , 2) AS fraction FROM ( -SELECT player_id, MIN(event_date) AS event_date -FROM Activity -GROUP BY player_id + SELECT player_id, MIN(event_date) AS event_date + FROM Activity + GROUP BY player_id ) AS A -LEFT JOIN -Activity AS B +LEFT JOIN Activity AS B ON A.player_id = B.player_id AND DATEDIFF(B.event_date, A.event_date) = 1 +