Skip to main content

LeetCode 550. Game Play Analysis IV SQL Solution

Problem

LeetCode SQL Problem

  1. Game Play Analysis IV

Activity table

player_iddevice_idevent_dategames_played
122016-03-015
122016-03-026
232017-06-251
312016-03-020
342018-07-035

Solution

Game Play Analysis IV
-- Get each player's first login date
WITH PlayerFirstLogin
AS (
SELECT player_id
,min(event_date) AS first_login_date
FROM Activity
GROUP BY player_id
)
,PlayerConsecutiveLogin
-- Get players players that logged in for at least two consecutive days starting from their first login date
AS (
SELECT A.player_id
FROM Activity AS A
INNER JOIN PlayerFirstLogin FL ON A.player_id = FL.player_id
AND datediff(A.event_date, FL.first_login_date) = 1
)
-- Report the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places.
SELECT cast((
SELECT count(DISTINCT player_id)
FROM PlayerConsecutiveLogin
) / cast((
SELECT count(DISTINCT player_id)
FROM Activity
) AS FLOAT) AS DECIMAL(10, 2)) AS fraction

Query Output

fraction
0.33