택시 이용 기록 테이블과 사람정보 테이블을 이용하여 날짜별 취소 비율을 계산해보자. 고객이나 기사가 블랙리스트에 올라가있다면 그 건수는 세지 않는다.
놀랍게도,, 테이블을 join하지 않고도 풀 수 있다. where 에 블랙리스트가 아닌 경우를 필터링 해주고 비율을 case when으로 설정해준다.
select request_at as Day,
round(count(case when status != 'completed' then 1 end)/count(id),2) as 'Cancellation Rate'
from trips
where client_id not in (select users_id from users where banned='Yes') and
driver_id not in (select users_id from users where banned='Yes') and
request_at between "2013-10-01" and "2013-10-03"
group by request_at ;
join하면 다음과 같이 나타낼 수 있겠다.
select request_at as Day,
round(count(case when status != 'completed' then 1 end)/count(id),2) as 'Cancellation Rate'
from (select id, a.client_id,a.driver_id,status, request_at from trips as a
inner join
(select * from users where banned='No' and role='client') as cl on a.client_id=cl.users_id
inner join
(select * from users where banned='No' and role='driver') as dr on a.driver_id=dr.users_id
) as final
where request_at between "2013-10-01" and "2013-10-03"
group by request_at
'learning > SQL' 카테고리의 다른 글
[LeetCode 196] Delete Duplicate Emails (0) | 2023.01.28 |
---|---|
[LeetCode 627] Swap Salary (0) | 2023.01.28 |
[LeetCode 1873] Calculate Special Bonus (0) | 2023.01.28 |
[LeetCode601] Human Traffic of Stadium (0) | 2023.01.23 |
[LeetCode185] Department Top Three Salaries (0) | 2023.01.21 |
댓글