OkBublewrap

1934. Confirmation Rate 본문

개발/SQL

1934. Confirmation Rate

옥뽁뽁 2025. 1. 21. 11:06

Confirmation Rate

문제

  1. 로그인 한 사람들 중 confirmed된 비율 구하기
  2. 없으면 0
  3. 소수점 2자리

입력 테이블

  1. Signups
    • user_id
    • time_stamp
  2. Confirmations
    • user_id
    • time_stamp
    • action

풀이

1. confirmation_rate 계산

confirmed 1로 합계 계산, user_id 기준 그룹화

select user_id,
    sum(case when action = 'confirmed' then 1 else 0 end) / count(*) as confirmation_rate
from Confirmations 
group by user_id 

2. Join계산

select s.user_id, -- 로그인 기준으로 조인
    round(coalesce(c.confirmation_rate, 0), 2) as confirmation_rate -- 2번째 계산
from Signups s
left join
(
    select user_id,
        sum(case when action = 'confirmed' then 1 else 0 end) / count(*) as confirmation_rate
    from Confirmations 
    group by user_id 
) c on s.user_id = c.user_id 

'개발 > SQL' 카테고리의 다른 글

180. Consecutive Numbers  (0) 2025.01.22
1070. Product Sales Analysis III  (0) 2025.01.22
1280. Students and Examinations  (0) 2025.01.20
기계당 평균 처리 시간  (0) 2025.01.17
상승하는온도  (0) 2025.01.16