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
'코딩테스트 > 문제풀이' 카테고리의 다른 글
[프로그래머스] 특정 기간동안 대여 가능한 자동차들의 대여비용 구하기 SQL (0) | 2023.10.03 |
---|---|
[프로그래머스] 가격대 별 상품 개수 구하기 SQL (0) | 2023.10.03 |
[프로그래머스] 입양 시각 구하기(2) SQL (1) | 2023.10.02 |
[프로그래머스] 년, 월, 성별 별 상품 구매 회원 수 구하기 SQL (0) | 2023.10.01 |
[프로그래머스] 우유와 요거트가 담긴 장바구니 SQL (0) | 2023.09.26 |