now is better than never

[LEVEL 4][JOIN] 특정 기간동안 대여 가능한 자동차들의 대여비용 구하기 본문

프로그래머스/SQL

[LEVEL 4][JOIN] 특정 기간동안 대여 가능한 자동차들의 대여비용 구하기

김초송 2023. 5. 15. 19:51

https://school.programmers.co.kr/learn/courses/30/lessons/157339

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

select c.car_id, c.car_type
    , round(c.daily_fee * (1 - d.discount_rate/100) * 30) fee
from CAR_RENTAL_COMPANY_CAR c join CAR_RENTAL_COMPANY_DISCOUNT_PLAN d
    on c.car_type = d.car_type
    join CAR_RENTAL_COMPANY_RENTAL_HISTORY  h 
    on c.car_id = h.car_id
where c.car_type IN ('세단', 'SUV')
    and d.duration_type = '30일 이상'
group by c.car_id
having sum(
            case when ((start_date < '2022-11-01' and end_date < '2022-11-01') 
                    or (start_date > '2022-11-30' and end_date > '2022-11-30'))
                then 0 else 1
            end
            ) = 0
         AND fee >= 500000 and fee < 2000000
ORDER BY fee desc, c.car_type, car_id desc

- having절 case when 을 조인할 때 필터링 하는게 나은지 having 절에서 필터링 하는게 나은지,,,?

- 그렇담 다른 조건들은 언제 필터링 하는게 나은지??