Search code examples
mysqlwindow-functions

Window function query issue?


Write an SQL query to report 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.

The query result format is in the following example.

Example 1:

Input: 
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-03-02 | 6            |
| 2         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+
Output: 
+-----------+
| fraction  |
+-----------+
| 0.33      |
+-----------+
Explanation: 
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

When using this query this leetcode problem is passing all test cases:

WITH temp AS(
    SELECT player_id,
    event_date - LAG(event_date, 1) OVER (PARTITION BY player_id ORDER BY event_date) AS difference, 
    RANK() OVER (PARTITION BY player_id ORDER BY event_date) as rn
    FROM activity
),

tp AS
(
    SELECT count(distinct(player_id)) as all_players
    FROM activity
)

SELECT ROUND(count(t.player_id)/tp.all_players,2) AS fraction

    FROM temp t
    JOIN tp

WHERE t.rn = 2
AND t.difference = 1

When I use this below query it's not working for all test cases can anyone tell me why this is not working where above one is working:

WITH temp AS(
    SELECT DISTINCT(player_id), difference FROM
    (SELECT player_id, event_date - LAG(event_date, 1) OVER (PARTITION BY player_id ORDER BY event_date) AS difference FROM activity) x WHERE x.difference = 1
),

tp AS
(
    SELECT count(distinct(player_id)) as all_players
    FROM activity
)

SELECT ROUND(COUNT(*)/tp.all_players, 2) as fraction FROM temp, tp;

Solution

  • You're asked to only count if their second login is consecutive. The first query accomplishes this by only counting the second row with a difference of 1.

    WHERE t.rn = 2
    AND t.difference = 1
    

    The second query will take any consecutive login.

    Demonstration


    Note that both queries need to group by tp.all_players. MySQL, in some modes, will sometimes infer the group by for you. But do not count on it, and other databases will not. See MySQL Handling of GROUP BY. Consider running MySQL in ANSI mode while learning SQL to make it better follow the standards.