|
| 1 | +# [Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv/description/?envType=study-plan-v2&envId=top-sql-50) |
| 2 | + |
| 3 | +### Problem Requirements: |
| 4 | + |
| 5 | + Find 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. |
| 6 | + |
| 7 | +<details> |
| 8 | +<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary> |
| 9 | + |
| 10 | +<details> |
| 11 | + <summary>Hint#1</summary> |
| 12 | + <p> |
| 13 | + What if you have another column that has the <code>first_login</code>, |
| 14 | + The problem will be very easy, isn't it? |
| 15 | + </p> |
| 16 | +</details> |
| 17 | +<details> |
| 18 | + <summary>Hint#2</summary> |
| 19 | + <p> |
| 20 | + Try use <code>WITH</code> clause. It will help you to define a temporary data set. It will make your life easier. |
| 21 | + </p> |
| 22 | +</details> |
| 23 | +<details> |
| 24 | + <summary>Hint#3</summary> |
| 25 | + <p> |
| 26 | + The SQL <code>MIN(expression)</code> function returns the minimum value in a set of values |
| 27 | + </p> |
| 28 | +</details> |
| 29 | +<details> |
| 30 | + <summary>Hint#4</summary> |
| 31 | + <p> |
| 32 | + Try use <code>window function</code>. |
| 33 | + </p> |
| 34 | +</details> |
| 35 | + |
| 36 | +<details> |
| 37 | + <summary>Hint#5</summary> |
| 38 | + <p>SQL has an aggregation function called <code>COUNT(expression)</code> which count all the rows that satisfy a specified condition</p> |
| 39 | +</details> |
| 40 | +<details> |
| 41 | + <summary>Hint#6</summary> |
| 42 | + <p> |
| 43 | + To subtract an interval e.g., a year, a month and a day to date, you can use the <code>DATE_SUB()</code> function. |
| 44 | + </p> |
| 45 | +</details> |
| 46 | +<details> |
| 47 | + <summary>Hint#7</summary> |
| 48 | + <p>SQL has a <code>ROUND(number, decimals)</code> function which rounds a number to a specified number of decimal places. </p> |
| 49 | +</details> |
| 50 | +</details> |
| 51 | + |
| 52 | +<details> |
| 53 | +<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary> |
| 54 | + |
| 55 | +We want to count the number of players who logged in the next day of their first login day. |
| 56 | +<br> |
| 57 | +we should determine the first login date for each player. |
| 58 | +Then when we find a player logged in the next day of this day we should put this player into account. |
| 59 | + |
| 60 | +<ul> |
| 61 | + <li>We can determine the first login for each player with the help of aggregate window function |
| 62 | + <code>MIN(expression) OVER </code> |
| 63 | + . Aggregate window function will work the same as the normal aggregate function but it doesn't cause rows to become grouped into a single output row. |
| 64 | + </li> |
| 65 | + <li>To be able to use our original table and the new column we can use <code> WITH</code> clause , it will help us to define the temporary data set and use them in the query. |
| 66 | + </li> |
| 67 | + <li> |
| 68 | + We will filter this table according to our condition <code>(event_date and first_login are consecutive days)</code>. |
| 69 | + <br> |
| 70 | + We can use <code>DATE_SUB()</code> function to subtract a given duration from <code>event_date</code> As follows<br> <code>DATE_SUB(event_date, INTERVAL 1 DAY) = first_login</code> |
| 71 | + </li> |
| 72 | + <li> |
| 73 | + Finally, divide the player that satisfies the condition by the total number of player that we can calculate as a <code>subquery</code> then round the result using <code>ROUND()</code>function to round the result. |
| 74 | + </li> |
| 75 | +</ul> |
| 76 | + |
| 77 | +</details> |
| 78 | + |
| 79 | +<details> |
| 80 | +<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary> |
| 81 | + |
| 82 | + |
| 83 | +```sql |
| 84 | +WITH temp AS( |
| 85 | + SELECT |
| 86 | + *, |
| 87 | + MIN(event_date) OVER (Partition BY player_id) AS first_login |
| 88 | + -- over means that the function before it is a window function |
| 89 | + FROM |
| 90 | + Activity |
| 91 | +) |
| 92 | +SELECT |
| 93 | + ROUND( |
| 94 | + ( |
| 95 | + SELECT |
| 96 | + COUNT(player_id) |
| 97 | + FROM |
| 98 | + temp |
| 99 | + WHERE |
| 100 | + DATE_SUB(event_date, INTERVAL 1 DAY) = first_login |
| 101 | + ) / ( |
| 102 | + SELECT |
| 103 | + COUNT(DISTINCT player_id) |
| 104 | + FROM |
| 105 | + Activity |
| 106 | + ), |
| 107 | + 2 |
| 108 | + ) AS fraction |
| 109 | + |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | +</details> |
0 commit comments