링크
https://leetcode.com/problems/confirmation-rate/description/
문제
The confirmation rate of a user is the number of 'confirmed' messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is 0. Round the confirmation rate to two decimal places.
Write a solution to find the confirmation rate of each user.
Return the result table in any order.
The result format is in the following example.
정답
select s.user_id,
round(avg(if(c.action="confirmed",1,0)),2) as confirmation_rate
from Signups as s
left join Confirmations as c
on s.user_id = c.user_id
group by user_id;
해설
- '확인' '비확인' 같이 두 가지 경우로 나뉠 경우 문자열을 숫자 1과 0으로 구분
- 1이 나온 평균을 구하여 비율을 구한다
새롭게 이해한 내용
- 상황에 따라 항목을 변경할 수 있다
'[업무 지식] > MySQL' 카테고리의 다른 글
[where & in ] Game Play Analysis IV (0) | 2024.10.30 |
---|---|
[sum & case when] Queries Quality and Percentage (0) | 2024.10.29 |
[Cross Join] Students and Examinations (0) | 2024.10.25 |
[같은 테이블 내 비교] Average Time of Process per Machine (0) | 2024.10.25 |
[on 조건] Rising Temperature (0) | 2024.10.24 |