Data Analyst/daily

[데이터 분석] 코드카타 98

rubus0304 2024. 11. 8. 10:07

98. Game Play Analysis IV

 

https://leetcode.com/problems/game-play-analysis-iv/description/

 

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 player_id
       count player_id fraction
from Activity
group by 1
where event_date...? (첫 로그인 날짜부터 최소 이틀 ..?  event_date 연속으로는 어떻게 구하는 건지 모르겠음.
 
 

(정답) 

 

with cnt_login as(

        select player_id,
                  datediff(event_date, min(event_date) over(Partition by player_id)) = 1 login
    from activity
)
select round(sum(login) / count(distinct player_id),2) as fraction
from cnt_login

 

개념정리

 

with문 쿼리

 

1. 최초 로그인한 날 Min(event_date)과 다음 로그인 한 날이 연속으로 있는지 알아보기위해  두 날짜의 차이가 1인 행 찾기 (Datediff)

https://extbrain.tistory.com/78

 

[MySQL] 날짜 차이 가져오기 (DATEDIFF, TIMESTAMPDIFF 함수)

▶MySQL 날짜 차이 가져오기 (DATEDIFF, TIMESTAMPDIFF 함수) ▶설명 MySQL에서 두 날짜간의 차이를 가져올 때 사용하는 함수가 두 가지가 있습니다. 단순히 일 차이를 가져올 때 사용하는 것이 DATEDIFF 함수

extbrain.tistory.com

 

2. player_id별로 로그인 여부를 파악해야 하므로 Partition by를 사용. (Group by를 하면 최초 또는 최대의 날짜밖에 구하지 못함)

https://jie0025.tistory.com/298

 

[MySQL] GROUP BY, PARTITION BY 차이점

GROUP BY와 PARTITION BY의 가장 큰 차이점은 행 수이다. ✅ GROUP BY GROUP BY 절은 특정 칼럼을 기준으로 집계 함수를 사용하여 건수(COUNT), 합계(SUM), 평균(AVG) 등 집 계성 데이터를 추출할 때 사용 group 에

jie0025.tistory.com

 

결과 쿼리

3. (연속 2일 로그인한 사용자 수/ 전체 사용자 수) 해주고 반올림