LeetCode 1097. Game Play Analysis V

1, Title Description

We define the install date of a player to be the first login day of that player.
We also define day 1 retention of some date X to be the number of players whose install date is X and they logged back in on the day right after X, divided by the number of players whose install date is X, rounded to 2 decimal places.
Write an SQL query that reports for each install date, the number of players that installed the game on that day and the day 1 retention.
The query result format is in the following example:

Activity table:

player_id device_id event_date games_played
 1 2016-03-01 
 1 2016-03-02 
 2 2017-06-25 
 2 2016-03-01 
 3 2016-07-03


Result table:

install_dt installs Day1_retention
2016-03-01 2 0.50
2017-06-25 1 0.00

 

Player 1 and 3 installed the game on 2016-03-01 but only player 1 logged back in on 2016-03-02 so the day 1 retention of 2016-03-01 is 1 / 2 = 0.50
Player 2 installed the game on 2017-06-25 but didn’t log back in on 2017-06-26 so the day 1 retention of 2017-06-25 is 0 / 1 = 0.00

2, problem-solving ideas

  • Construction of the table statement:
 1 CREATE TABLE Activity (
 2   player_id INT (8),
 3   event_date VARCHAR (20)
 4 ) ;
 5 
 6 INSERT INTO Activity (player_id, event_date) 
 7 VALUES
 8   (1, '2018-01-01') ;
 9 
10 INSERT INTO Activity (player_id, event_date) 
11 VALUES
12   (1, '2018-01-02') ;
13 
14 INSERT INTO Activity (player_id, event_date) 
15 VALUES
16   (2, '2018-01-01') ;
17 
18 INSERT INTO Activity (player_id, event_date) 
19 VALUES
20   (2, '2019-03-01') ;
21 
22 INSERT INTO Activity (player_id, event_date) 
23 VALUES
24   (2, '2019-03-02') ;
25 
26 INSERT INTO Activity (player_id, event_date) 
27 VALUES
28   (3, '2019-02-28') ;
29 
30 INSERT INTO Activity (player_id, event_date) 
31 VALUES
32   (3, '2019-03-01') ;
33   
34 INSERT INTO Activity (player_id, event_date) 
35 VALUES
36   (4, '2019-02-25') ;
37 
38 INSERT INTO Activity (player_id, event_date) 
39 VALUES
40   (4, '2019-03-01') ;
41  
42 INSERT INTO Activity (player_id, event_date) 
43 VALUES
44   (5, '2019-03-05') ;

 

  • Queries daily number of players the game is installed:
 1 SELECT 
 2   event_date,
 3   COUNT(1) AS installs 
 4 FROM
 5   (SELECT 
 6     player_id,
 7     event_date 
 8   FROM
 9     Activity 
10   GROUP BY player_id 
11   ORDER BY player_id,
12     event_date) a 
13 GROUP BY event_date;

 

  • According to Visit in ascending order, are numbered for each player's Event Log:

       If you are using a database support ROW_NUMBER () function can be more elegant implementation of this step.

 1 SELECT 
 2     player_id,
 3     event_date,
 4     IF(
 5       @id = player_id,
 6       @rank := @rank + 1,
 7       @rank := 1
 8     ) AS rank,
 9     @id := player_id 
10   FROM
11     Activity,
12     (SELECT 
13       @id := NULL,
14       @rank := 0) r 
15   ORDER BY player_id,
16     event_date ;

 

  • Statistics install the game after the second day still landing the game player information:

  Spend an intermediate step from related tables, see the SQL statements associated with the condition.

 1 SELECT 
 2   *
 3 FROM
 4   (SELECT 
 5     player_id,
 6     event_date,
 7     IF(
 8       @id = player_id,
 9       @rank := @rank + 1,
10       @rank := 1
11     ) AS rank,
12     @id := player_id 
13   FROM
14     Activity,
15     (SELECT 
16       @id := NULL,
17       @rank := 0) r 
18   ORDER BY player_id,
19     event_date) t1,
20     (SELECT 
21     player_id,
22     event_date,
23     IF(
24       @id2 = player_id,
25       @rank2 := @rank2 + 1,
26       @rank2 := 1
27     ) AS rank,
28     @id2 := player_id 
29   FROM
30     Activity,
31     (SELECT 
32       @id2 := NULL,
33       @rank2 := 0) r 
34   ORDER BY player_id,
35     event_date) t2  
36 WHERE t1.rank = 1 
37 AND t2.rank =2
38 AND t1.player_id = t2.player_id
39 AND DATEDIFF(t2.event_date,t1.event_date)=1;

 

  • Complete the query:
 1 SELECT 
 2   temp1.event_date AS install_dt,
 3   temp1.installs,
 4   ROUND(
 5     IFNULL(temp2.counts, 0) / temp1.installs,
 6     2
 7   ) AS Day1_retention 
 8 FROM
 9   (SELECT 
10     event_date,
11     COUNT(1) AS installs 
12   FROM
13     (SELECT 
14       player_id,
15       event_date 
16     FROM
17       Activity 
18     GROUP BY player_id 
19     ORDER BY player_id,
20       event_date) a 
21   GROUP BY event_date) temp1 
22   LEFT JOIN 
23     (SELECT 
24       t1.event_date,
25       COUNT(1) AS counts 
26     FROM
27       (SELECT 
28         player_id,
29         event_date,
30         IF(
31           @id = player_id,
32           @rank := @rank + 1,
33           @rank := 1
34         ) AS rank,
35         @id := player_id 
36       FROM
37         Activity,
38         (SELECT 
39           @id := NULL,
40           @rank := 0) r 
41       ORDER BY player_id,
42         event_date) t1,
43       (SELECT 
44         player_id,
45         event_date,
46         IF(
47           @id2 = player_id,
48           @rank2 := @rank2 + 1,
49           @rank2 := 1
50         ) AS rank,
51         @id2 := player_id 
52       FROM
53         Activity,
54         (SELECT 
55           @id2 := NULL,
56           @rank2 := 0) r 
57       ORDER BY player_id,
58         event_date) t2 
59     WHERE t1.rank = 1 
60       AND t2.rank = 2 
61       AND t1.player_id = t2.player_id 
62       AND DATEDIFF(t2.event_date, t1.event_date) = 1 
63     GROUP BY t1.event_date) temp2 
64     ON temp1.event_date = temp2.event_date ;

 

3 Summary

      This knowledge has the following problems:

  1. How packets are numbered sequentially. This is a common demand, the easiest way is to realize row_number function, but this function is not mysql, needs its own use sql statement to achieve.
  2. Use date functions.
  3. Connected to the left null value determination.

Guess you like

Origin www.cnblogs.com/onePunchCoder/p/11601074.html