코딩테스트

[프로그래머스] 특정 기간 동안 대여 가능한 차들의 비용 구하기 (mysql)

코앤미 2023. 7. 20. 10:14

 

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

 

프로그래머스

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

programmers.co.kr

 

 

적절 히 조건을 분기한 뒤, subquery를 통해 구했다.

select ccdd.car_id , ccdd.car_type,
truncate ( (ccdd.daily_fee*(100-ccdd.discount_rate)/100)*30,0 ) as fee
from 
(select car_id,daily_fee, discount_rate,crcc.car_type # 할인 후에 50~200만 사이인 차
from CAR_RENTAL_COMPANY_CAR as crcc  join CAR_RENTAL_COMPANY_DISCOUNT_PLAN as crcd
where crcc.car_type = crcd.car_type 
and  duration_type ="30일 이상"
and (crcc.car_type ="세단" or crcc.car_type="SUV") ) as ccdd

where 
car_id not in 	#2022-11-01~2022-11-30 동안 대여 가능한 차.
(SELECT car_id
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where substring(start_date,1,10) < '2022-11-31' and substring(end_date,1,10) > '2022-11-00')

# HAVING 에서 SELECT의 ALIAS(fee) 가 적용되는건 MYSQL에서만 허용되는 예외적 문법이다.
having  499999<fee and fee <2000000 

order by fee desc, ccdd.car_type asc, ccdd.car_id desc