[LeetCode] 550. Game Play Analysis IV
๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/game-play-analysis-iv/description/
Game Play Analysis IV - LeetCode
Can you solve this real interview question? Game Play Analysis IV - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
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) ์ ์ผ์ ์ฐจ์ด