Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[LeetCode] 550. Game Play Analysis IV

์ธ” 2023. 4. 21. 15:30

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ 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) ์˜ ์ผ์ž ์ฐจ์ด