๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/game-play-analysis-iv/description/
Table : Activity
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.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ฒ์ ๋ก๊ทธ์ธํ ๋ค์๋ ๋ค์ ๋ก๊ทธ์ธํ ํ๋ ์ด์ด์ ๋น์จ(์์์ 2์๋ฆฌ ๋ฐ์ฌ๋ฆผ)์ ๊ตฌํด๋ผ
์ฆ, ( ์ฒ์ ๋ก๊ทธ์ธํ๊ณ ์ดํ์งธ ์ฐ์ ๋ก๊ทธ์ธํ ํ๋ ์ด์ด / ์ ์ฒด ํ๋ ์ด์ด ) ๋ฅผ ๊ตฌํด์ผ ํ๋ค.
ํ๋ ์ด์ด๋ณ ์ฒซ ๋ก๊ทธ์ธ ์ผ์๋ฅผ ์์ ํ
์ด๋ธ๋ก ์ถ๋ ฅํ๊ณ ๋ฉ์ธ ์ฟผ๋ฆฌ ์กฐ๊ฑด์ ์ Activity ํ
์ด๋ธ๊ณผ ์์ ํ
์ด๋ธ์ player_id ๊ธฐ์ค์ผ๋ก ์กฐ์ธํด์
์ฒซ ๋ก๊ทธ์ธ ์ผ์์ ๋ก๊ทธ์ธ ์ผ์ ์ฐจ์ด๊ฐ 1์ผ์ ์ค์ ํ๋ค.ํ๋ ์ด์ด์ ๋น์จ์ ๊ตฌํด์ผ ํ๋ฏ๋ก ์ฌ๋ฌ๋ฒ ์ ์ํ ๊ฒ์ ๋ํ ์ค๋ณต์ COUNT์์ DISTINCT๋ก ์ ๊ฑฐํด์ค์ผํ๋ค.
WITH ACT1 AS (
SELECT player_id, MIN(event_date) AS min_log
FROM ACTIVITY
GROUP BY 1
)
SELECT ROUND(COUNT(DISTINCT T1.player_id) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction
FROM ACT1 T1
JOIN Activity T2
ON T1.player_id = T2.player_id
AND DATEDIFF(T1.min_log, T2.event_date) = -1
* DATEDIFF(๋ ์ง1, ๋ ์ง2)
→ (๋ ์ง1 - ๋ ์ง2) ์ ์ผ์ ์ฐจ์ด
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 585. Investments in 2016 (0) | 2023.05.09 |
---|---|
[LeetCode] 570. Managers with at Least 5 Direct Reports (0) | 2023.04.24 |
[LeetCode] 607. Sales Person (0) | 2023.04.20 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์กฐ๊ฑด์ ๋ง๋ ์ฌ์ฉ์์ ์ด ๊ฑฐ๋๊ธ์ก ์กฐํํ๊ธฐ (0) | 2023.04.07 |
[DATA ON AIR/ ํด์ฆ๋ก ๋ฐฐ์ฐ๋ SQL] ์ฐ์๋ ๋ ์ง๋ฅผ ํ๋์ ๊ทธ๋ฃน์ผ๋ก ํํํด๋ณด์ (0) | 2023.03.29 |