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

 

프로그래머스

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

programmers.co.kr

 

풀이

 

1. car_rental_company_rental_history 테이블과 car_rental_company_car 테이블을 inner join

case 문으로 7일, 30일, 90일 이상의 값을 가진 duration 속성 출력 (7일 미만일 경우 null)

 

2. 1번에서 나온 테이블과 car_rental_company_discount_plan left outer join

duration 이 null 인 경우의 history 도 출력

 

3. case 문 한 번 더 써서 duration_type 이 null 인 경우는 daily_fee 와 date 를 곱하고

아닐 경우는 discount_rate 만큼의 할인이 적용된 가격 출력

 

select aa.history_id,
case
when duration_type is null then aa.daily_fee * date
else 
 floor((aa.daily_fee * date)-((aa.daily_fee * date) * discount_rate/100))
end as fee
from
(
    select car.car_id ,history.history_id, car.daily_fee as daily_fee, datediff(history.end_date, history.start_date)+1 as date,
    case
    when  datediff(history.end_date, history.start_date)+1 between 7 and 30 then '7일 이상'
    when datediff(history.end_date, history.start_date)+1 between 30 and 90 then '30일 이상'
    when datediff(history.end_date, history.start_date)+1 >= 90 then '90일 이상'
    end as duration
    from car_rental_company_car as car, car_rental_company_rental_history as history
    where car.car_id = history.car_id and car.car_type like '트럭'
) as aa
left outer join
car_rental_company_discount_plan as discount
on aa.duration = discount.duration_type and discount.car_type = '트럭'
order by fee desc, history.history_id desc

 

+ Recent posts