테이블 정보
문제
The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.
Write a solution to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03". Round Cancellation Rate to two decimal points.
Return the result table in any order.
The result format is in the following example.
결과 예시
정답 코드 (MySQL)
select
request_at as 'Day',
Round(sum(status <> 'completed') / count(status), 2) as 'Cancellation Rate'
from Trips
where request_at between '2013-10-01' and '2013-10-03' and
client_id NOT IN (select users_id
from Users
where role = 'client' and banned = 'Yes') and
driver_id NOT IN (select users_id
from Users
where role = 'driver' and banned = 'Yes')
group by request_at;
문제 풀이
취소율은 금지되지 않은 사용자가 있는 요청의 수를
해당 날짜의 금지되지 않은 사용자가 있는 요청의 총 수로 나누어 계산한다.
여기서 금지되지 않은 사용자는 Users 테이블에서 banned 컬럼 값이 'No'에 해당하는 클라이언트, 드라이버들을 말한다.
그러므로 banned된 사용자는 집계에서 제외하기 위해 다중행 서브 쿼리를 사용하고,
"2013-10-01"에서 "2013-10-03" 사이에서
각각 계산한 취소율을 Round 함수를 사용하여 소수점 두 자리까지 나타내어 계산해주면 된다.
Trips and Users - LeetCode
Can you solve this real interview question? Trips and Users - 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
'SQL 문제 풀이' 카테고리의 다른 글
[프로그래머스 Level 4] 자동차 대여 기록 별 대여 금액 구하기 (0) | 2024.02.14 |
---|---|
[프로그래머스 Level 4] 우유와 요거트가 담긴 장바구니 (0) | 2024.01.25 |
[LeetCode Hard] 601. Human Traffic of Stadium (0) | 2024.01.16 |
[LeetCode Medium] 1341. Movie Rating (0) | 2024.01.12 |
[LeetCode Hard] 185. Department Top Three Salaries (2) | 2024.01.07 |