02/27 SQL 문제풀이 - 550. Game Play Analysis IV (Leetcode)

Data Architect / Engineer·2024년 2월 27일
1

1일_1SQL

목록 보기
40/63
post-thumbnail

문제

  • LeetCode SQL 문제
  • 550. Game Play Analysis IV / Medium
  • 문제 내용 : [링크]


내가 작성한 Query

with temp_01 as(
    select player_id, min(event_date) as event_date
    from activity
    group by player_id
)
select round(count(distinct b.player_id) / count(distinct a.player_id), 2) as fraction
from activity a
    left join temp_01 b on datediff(a.event_date, b.event_date) = 1 and a.player_id = b.player_id
  • 첫 방문 이후, 바로 다음날 방문한 고객의 비율을 구하는 문제이다.

  • 먼저 player_id별 최초 방문 날짜(min(event_date))를 구해준다. 이 데이터를 temp_01로 지정한다.

  • activity 테이블과 위의 temp_01 테이블을 JOIN 해준다. 단, 총 유저 수를 구해줘야 하므로, 첫 방문 이후에 방문기록이 없는 유저 데이터 레벨을 살려주어야 한다. 그러므로 activity 테이블 레벨에 맞게 LEFT JOIN 해 준다.

  • 이때 같은 유저 activityevent_datetemp_01event_date의 차이가 1일인 조건을 걸어준다.

  • 전체 유저 수 count(distinct a.player_id) 기준 첫 방문 이후 다음날 방문한 기록이 있는 유저수 count(distinct b.player_id)의 비율을 구해준다. 이 값을 fraction으로 출력해준다.
    (LEFT JOIN 해 주었으므로, a.player_id의 수를 구하면, 첫 방문 이후 기록이 없는 유저의 수를 구할 수 있다.)

  • round() 함수를 통해 소수 둘째자리까지 출력해준다.

  • 🌟🌟 중요 : 데이터 출력 레벨에 따라 LEFT JOIN 할 지, JOIN 할 지 정해진다. 살려야 하는 데이터 레벨 기준에 따라 적절히 선택할 것!

profile
질문은 계속돼 아오에

0개의 댓글