๋ฌธ์
https://leetcode.com/problems/trips-and-users/
์ฝ๋ ๋ต์
with result as (
select id, client_id, driver_id, status, request_at
from trips t
join (select users_id, banned from users where role = 'client') c
on t.client_id = c.users_id
join (select users_id, banned from users where role = 'driver') d
on t.driver_id = d.users_id
where c.banned = 'No'
and d.banned = 'No'
),
request_rate as (
select request_at as Day,
count(case when status = 'cancelled_by_driver' or status = 'cancelled_by_client' then id else null end) as can_res,
count(id) as all_res
from result
where request_at between '2013-10-01' and '2013-10-03'
group by request_at
)
select Day,
round(can_res / all_res, 2) as 'Cancellation Rate'
from request_rate
^ 25๋ถ์ด ๊ฑธ๋ฆฐ ๋์ ์ง์ ๋ถํ ์ฝ๋ ..
select
request_at as Day,
round(sum(if(status = 'completed', 0, 1)) / count(id), 2) as 'Cancellation Rate'
from trips
where client_id in (select users_id from users where role = 'client' and banned = 'No')
and driver_id in (select users_id from users where role = 'driver' and banned = 'No')
and request_at between '2013-10-01' and '2013-10-03'
group by request_at
^ ๋ค๋ฅธ ๋ฐ์ ๊ฐ์ ธ์จ ๊น์ํ ์ฝ๋
์ด๋์ ๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ๊ณต๋ถํ๋ ๊ฒ ์ค์ํ ๊ฒ ๊ฐ๋ค!
ํต์ฌ ์์ง
1. ๋ฐ์ดํฐ ๊ฐ๊ณต์ ์ง๊ณ๊ฐ ์๋๋ฉด with์ ์ ์ฌ์ฉํ ์์ ํ ์ด๋ธ์ ๋ง๋ค๊ธฐ๋ณด๋ค๋ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ด์ฉํด๋ณด์ !
b ํ ์ด๋ธ์ ์๋ ์ ๋ณด๋ก ์กฐ๊ฑด์ ๊ฑธ์ด์ a ํ ์ด๋ธ์ ํ์ฉํ๊ณ ์ถ์ผ๋ฉด
where a.์ปฌ๋ผ in (bํ ์ด๋ธ์ select๋ฌธ)
-> ์ด ๋ฌธ๋ฒ์ ์ ๊ทน์ ์ผ๋ก ์ฌ์ฉํด๋ณด์.
์์ํ ์ด๋ธ์ ๋ง๋ค ํ์๋ ์์ด ๋น ๋ฅด๊ฒ ์ฝ๋๋ฅผ ์์ฑ์ํฌ ์ ์๋ค.
๋๋ ๋๋ฌด ๋ฌดํฑ๋๊ณ with๋ก ์์ํ ์ด๋ธ์ ๋จผ์ ๋ง๋ค๊ณ ๋ณด๋ ๋ฏ ..;
2. if ( ์กฐ๊ฑด, ์ฐธ์ผ ๋ ๊ฐ, ๊ฑฐ์ง์ผ ๋ ๊ฐ)
๋๋ sql์๋ if๋ฌธ์ ์ฌ์ฉํ ์ ์๋์ง ๋ชฐ๋๋ค ; ์๋ ๊น๋จน๊ณ ์์๋ค
๊ณ์ case๋ฌธ๋ง ์ฃผ๊ตฌ์ฅ์ฐฝ ๋๋ ธ๋๋ฐ;
์กฐ๊ฑด์ด ํ๊ฐ์ผ ๊ฒฝ์ฐ์๋ if๋ฌธ์ ์ฌ์ฉํ์ !
๋๋ ์
๋ฌธ์ ๊ฐ ๋์ด๋๊ฐ ๋๋ค๊ณ
์ ๋ต์ด ๊ตฌ๊ตฌ์ ์ ํ ์ฝ๋์ผ ํ์๋ ์๋ค
์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์ ์
์ด๋ป๊ฒ ์งค์ง ๊ตฌ์กฐ๋ฅผ ๋จธ๋ฆฟ์์ ํ๋ฒ ๊ตฌ์ํด์ ํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
๊ทผ๋ฐ ์๊ฐ ์๋ฐ ๋๋ฌธ์ ๋ง์์ด ๊ธํด์ ธ์ ์ฝ์ง๋ ์๋ค ใ