๐ป ๋ฌธ์ ์ฃผ์ : Game Play Analysis IV - LeetCode
Game Play Analysis IV - LeetCode
Can you solve this real interview question? Game Play Analysis IV - Table: Activity +--------------+---------+ | Column Name | Type | +--------------+---------+ | player_id | int | | device_id | int | | event_date | date | | games_played | int | +---------
leetcode.com
๐ป ๋ฌธ์
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.
๐ป ์ฝ๋ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
select round(count(distinct a1.player_id) / (select count(distinct player_id) from Activity), 2) as fraction
from Activity a1
join Activity a2
on a1.player_id = a2.player_id and datediff(a2.event_date, a1.event_date) = 1
where (a1.player_id, a1.event_date) in (select player_id, min(event_date) from Activity group by player_id)
๐ป ๋ฌธ์ ํ์ด
์ ๋ฌธ์ ๋ ๋ค์๊ณผ ๊ฐ์ ์กฐ๊ฑด์ ๊ฐ์ง๊ณ ์๋ค.
1๏ธโฃ ์ฒซ ๋ฒ์งธ ๋ก๊ทธ์ธ ๋ ๊ณผ ๋ ๋ฒ์งธ ๋ก๊ทธ์ธ ๋ ์ด ํ๋ฃจ ์ฐจ์ด๋ก ์ฐ์์ ์ผ ๊ฒ
2๏ธโฃ 1๏ธโฃ์กฐ๊ฑด์ ํด๋นํ๋ ๋ฐ์ดํฐ ๋น์จ ๊ตฌํ๊ธฐ
3๏ธโฃ 2๏ธโฃ์์ ๊ตฌํ ๋น์จ์ ์์ซ์ ๋ ์๋ฆฌ๊น์ง ์ถ๋ ฅ
1๏ธโฃ ์ฒซ ๋ฒ์งธ ๋ก๊ทธ์ธ ๋ ๊ณผ ๋ ๋ฒ์งธ ๋ก๊ทธ์ธ ๋ ์ด ํ๋ฃจ ์ฐจ์ด๋ก ์ฐ์์ ์ผ ๊ฒ
์ด ๋ฌธ์ ์ ํต์ฌ ์กฐ๊ฑด์ผ๋ก, ์ฐ์์ผ๋ก ๋ก๊ทธ์ธํ ์ฌ๋์ id๋ฅผ ๊ตฌํด์ผ ํ๋ค. ์ด๋ฅผ ์ํด์ ์ฐ๋ฆฌ๋ ๋ก๊ทธ์ธํ ๋ ๊ฐ์ ์ฐจ์ด๋ฅผ ๊ตฌํ ์ ์๋ ํจ์๋ค ์ค datediff() ํจ์๋ฅผ ์ฌ์ฉํ ๊ฒ์ด๋ค.
datediff(date2, date1) ํจ์๋ (date2 - date1) ๊ฐ์ ๋ฐํํ๋ ํจ์๋ก, ์ ๋ฌธ์ ์์๋ '์ฐ์'์ผ๋ก ๋ก๊ทธ์ธํ๋ ๊ฒ์ ์กฐ๊ฑด์ผ๋ก ๊ฑธ์๊ธฐ ๋๋ฌธ์ ์ด ์ฐจ์ด๊ฐ 1์ด ๋์ด์ผ ํ๋ค. ๋ํ 'ํ ์ฌ๋'์ด ๊ฐ์ id๋ก ์ฐ์ ๋ก๊ทธ์ธํด์ผ ํ๊ธฐ ๋๋ฌธ์ player_id ๋ํ ๊ฐ์์ผ ํ๋ค.
select
from Activity a1
join Activity a2
on a1.player_id = a2.player_id and datediff(a2.event_date, a1.event_date) = 1
2๏ธโฃ 1๏ธโฃ์กฐ๊ฑด์ ํด๋นํ๋ ๋ฐ์ดํฐ ๋น์จ ๊ตฌํ๊ธฐ
์ self join์ด ์คํ๋๋ฉด Activity ํ ์ด๋ธ์๋ ์ฐ์์ผ๋ก ๋ก๊ทธ์ธํ ์ฌ๋์ ๋ฐ์ดํฐ๋ง ๋จ๊ฒ ๋๋ค. ๋ฐ๋ผ์ ์ฐจ์ง ๋น์จ์ ๊ตฌํ๊ธฐ ์ํ '์ ์ฒด' ์ฌ๋์ ์๋ ๊ตฌํ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์ฒด ์ฌ๋์ ์๋ฅผ ๋ฐ๋ก ์ถ์ถํด์ค์ผ ํ๋ค.
select count(distinct a1.player_id) / (select count(distinct player_id) from Activity)
from Activity a1
join Activity a2
on a1.player_id = a2.player_id and datediff(a2.event_date, a1.event_date) = 1
3๏ธโฃ 2๏ธโฃ์์ ๊ตฌํ ๋น์จ์ ์์ซ์ ๋ ์๋ฆฌ๊น์ง ์ถ๋ ฅ
์ด์ Leetcode์์ ์ต์ํ ์์ซ์ ์กฐ๊ฑด์ด ๋์๋ค. ์ง๊ธ๊น์ง ํด์๋ ๋๋ก round() ํจ์๋ฅผ ํตํด ๋ ์๋ฆฌ๊น์ง ์ถ๋ ฅํ๊ณ fraction์ผ๋ก ์ด๋ฆ์ ๋ฐ๊พธ์ด ์ฃผ๋ฉด ๋๋ค.
select round(count(distinct a1.player_id) / (select count(distinct player_id) from Activity), 2) as fraction
from Activity a1
join Activity a2
on a1.player_id = a2.player_id and datediff(a2.event_date, a1.event_date) = 1
์ฌ๊ธฐ๊น์ง ํ๋ฉด ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ์ด ์ค๋ต์ผ๋ก ์ฑ์ ๋๋ค.

ํด๊ฒฐ์ ์ ๋ฐ๋ก 1๏ธโฃ์ ์๋ค.
๋ฌธ์ ์์๋ ํ ์ฐจ๋ก ์ธ๊ธํ๋ฏ, fraction = logged in again on the day after the day they first logged in ์ผ๋ก, ์ฒซ ๋ฒ์งธ ๋ก๊ทธ์ธ์ ํ ์ดํ ๋ค์ ๋ ์ fraction์ด๋ผ๊ณ ํ๋ค. Test case2์์ ์ ๋ ฅ๋ ๋ฐ์ดํฐ ์ค์๋ ์ฒซ์งธ ๋ ๋ถํฐ ์ ์งธ ๋ ๊น์ง ์ฐ์์ ์ผ๋ก ๋ก๊ทธ์ธ์ ํ ์ผ์ด์ค๊ฐ ์๋๋ฐ ์ด ๊ฒฝ์ฐ, ์ฒซ์งธ-๋์งธ ๋ ๋ง ์นด์ดํธ๋ฅผ ํด์ผ ํจ์๋ ๋ถ๊ตฌํ๊ณ ๋์งธ-์ ์งธ ๋ ๋ ์นด์ดํธ๋ฅผ ํ๊ธฐ์ Expected ๊ฐ๋ณด๋ค ๋ ๋ง์ ๊ฐ์ด ๋์จ ๊ฒ์ด๋ค.
๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ '์ฒซ ๋ฒ์งธ'๋ก ๋ก๊ทธ์ธํ ๋ ์ ๊ธฐ์ค์ผ๋ก ์ผ๊ธฐ ์ํด Join์ผ๋ก ๋ง๋ ํ ์ด๋ธ์ where๋ก ์กฐ๊ฑด์ ๊ฑธ ๊ฒ์ด๋ค.
select round(count(distinct a1.player_id) / (select count(distinct player_id) from Activity), 2) as fraction
from Activity a1
join Activity a2
on a1.player_id = a2.player_id and datediff(a2.event_date, a1.event_date) = 1
where (a1.player_id, a1.event_date)
in (select player_id, min(event_date) from Activity group by player_id)
min() ํจ์๋ฅผ ๋ฃ์ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ํตํด ๊ฐ ์ ์ ๋ณ๋ก ์ฒ์ ๋ก๊ทธ์ธํ ๋ ์ ๊ตฌํ๊ณ , ์ด๋ค ์ค ์ฐ์์ ์ผ๋ก ๋ก๊ทธ์ธํ ์ ์ id๊ฐ ์๋์ง๋ฅผ ๊ฒํ ํ๋ ์ฟผ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ฉด ๋น๋ก์ Accepted๊ฐ ๋จ๋ฉฐ ๋๋๋ค.
๐ป Beats

'SQL > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode][SQL50] #24. User Activity for the Past 30 Days I (1) | 2024.01.04 |
---|---|
[LeetCode][SQL50] #23. Number of Unique Subjects Taught by Each Teacher (1) | 2024.01.03 |
[LeetCode][SQL50] #21. Immediate Food Delivery II (1) | 2024.01.01 |
[LeetCode][SQL50] #20. Monthly Transactions I (0) | 2023.11.16 |
[LeetCode][SQL50] #19. Queries Quality and Percentage (0) | 2023.11.15 |