Skip to content

Commit 19b9477

Browse files
committed
Upload 116
1 parent 365bc4d commit 19b9477

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ To make the daily tasks of creating a new file and updating the index easier, th
144144
| 113 | [Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage/description/) | [Solution](solutions/113_queries_quality_and_percentage.md) | LeetCode | Easy | |
145145
| 114 | [Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i/description/) | [Solution](solutions/114_monthly_transactions_i.md) | LeetCode | Medium | |
146146
| 115 | [Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii/description/) | [Solution](solutions/115_immediate_food_delivery_ii.md) | LeetCode | Medium | |
147+
| 116 | [Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv/description/) | [Solution](solutions/116_game_play_analysis_iv.md) | LeetCode | Medium | |
147148

148149
## Author(s)
149150

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# SQL Everyday \#116
2+
3+
## Game Play Analysis IV
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
Write a solution 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. [[Full Description](https://leetcode.com/problems/game-play-analysis-iv/description/)]
11+
12+
## Submitted Solution
13+
14+
```sql
15+
-- Submitted Solution
16+
WITH cte AS
17+
(
18+
SELECT
19+
player_id
20+
,DATE(MIN(event_date) + INTERVAL 1 DAY) AS second_day
21+
FROM Activity
22+
GROUP BY player_id
23+
)
24+
SELECT
25+
ROUND(COUNT(*) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction
26+
FROM Activity a
27+
JOIN cte ON a.player_id = cte.player_id AND a.event_date = cte.second_day
28+
;
29+
```
30+
31+
## Site Solution
32+
33+
```sql
34+
-- LeetCode Solution
35+
-- Approach 1: Subqueries and multi-value use of the IN comparison operator
36+
SELECT
37+
ROUND(
38+
COUNT(A1.player_id)
39+
/ (SELECT COUNT(DISTINCT A3.player_id) FROM Activity A3)
40+
, 2) AS fraction
41+
FROM
42+
Activity A1
43+
WHERE
44+
(A1.player_id, DATE_SUB(A1.event_date, INTERVAL 1 DAY)) IN (
45+
SELECT
46+
A2.player_id,
47+
MIN(A2.event_date)
48+
FROM
49+
Activity A2
50+
GROUP BY
51+
A2.player_id
52+
);
53+
54+
-- Approach 2: CTEs and INNER JOIN
55+
WITH first_logins AS (
56+
SELECT
57+
A.player_id,
58+
MIN(A.event_date) AS first_login
59+
FROM
60+
Activity A
61+
GROUP BY
62+
A.player_id
63+
), consec_logins AS (
64+
SELECT
65+
COUNT(A.player_id) AS num_logins
66+
FROM
67+
first_logins F
68+
INNER JOIN Activity A ON F.player_id = A.player_id
69+
AND F.first_login = DATE_SUB(A.event_date, INTERVAL 1 DAY)
70+
)
71+
SELECT
72+
ROUND(
73+
(SELECT C.num_logins FROM consec_logins C)
74+
/ (SELECT COUNT(F.player_id) FROM first_logins F)
75+
, 2) AS fraction;
76+
```
77+
78+
## Notes
79+
80+
TODO
81+
82+
Go to [Table of Contents](/README.md#contents)\
83+
Go to [Overview](/README.md)

0 commit comments

Comments
 (0)