[SQL34] Calculate the number of players who have logged in for at least two consecutive days and divide this number by the total number of players

Write a SQL query to report the scores of players who log in again the day after the first log in, rounded to two decimal places. In other words, you need to count the number of players who have logged in for at least two consecutive days starting from the first login date, and divide this number by the total number of players.

The query result format is as follows:
Activity table:
image.png

Result table:

Result table:
image.png

Explanation: Only the player with ID 1 will log in again after logging in on the first day, so the answer is 1/3=0.33

 

solve:

SELECT round((SELECT COUNT(DISTINCT b.player_id)
                FROM activity AS b
                    ,activity AS c
               WHERE b.event_date =
                     (SELECT MIN(temp.event_date) FROM activity AS temp WHERE temp.player_id = b.player_id)
                 AND b.player_id = c.player_id
                 AND datediff(b.event_date, c.event_date) = -1) / COUNT(DISTINCT a.player_id),
             2) AS 'fraction'
  FROM activity AS a;




 

Guess you like

Origin blog.csdn.net/debimeng/article/details/104347728