Link
Tables
Question
CAR_RENTAL_COMPANY_CAR 테이블과 CAR_RENTAL_COMPANY_RENTAL_HISTORY 테이블과 CAR_RENTAL_COMPANY_DISCOUNT_PLAN 테이블에서 자동차 종류가 '세단' 또는 'SUV' 인 자동차 중 2022년 11월 1일부터 2022년 11월 30일까지 대여 가능하고 30일간의 대여 금액이 50만원 이상 200만원 미만인 자동차에 대해서 자동차 ID, 자동차 종류, 대여 금액(컬럼명: FEE) 리스트를 출력하는 SQL문을 작성해주세요. 결과는 대여 금액을 기준으로 내림차순 정렬하고, 대여 금액이 같은 경우 자동차 종류를 기준으로 오름차순 정렬, 자동차 종류까지 같은 경우 자동차 ID를 기준으로 내림차순 정렬해주세요.
DataFlow
•
세단, suv 자동차
•
11월 1일 부터 30일까지 대여가능
•
30일 대여금액 → 50만원 이상 200만원 미만
Answer
with car_table as (
select *
from car_rental_company_car
where car_type in ('세단', 'SUV')
),
car_history as (
select
c.*,
h.start_date,
h.end_date
from car_table c
left join car_rental_company_rental_history h
on c.car_id = h.car_id
)
select
distinct h.car_id,
h.car_type,
round((h.daily_fee * 30 * (1 - (p.discount_rate * 0.01))), 0) as fee
from car_history h
join car_rental_company_discount_plan p
on h.car_type = p.car_type
and p.duration_type = '30일 이상'
where (h.daily_fee * 30 * (1 - (p.discount_rate * 0.01))) between 500000 and 2000000
and h.car_id not in (select car_id from car_history where date_format(end_date, '%Y-%m-%d') >= date_format('2022-11-01', '%Y-%m-%d'))
order by (h.daily_fee * 30 * (1 - (p.discount_rate * 0.01))) desc, h.car_type asc, h.car_id desc;
SQL
복사
Intention
•
(h.daily_fee * 30 * (1 - (p.discount_rate * 0.01))) 같은 수식 반복
•
대여 가능한 날짜를 찾아내는 과정이 제일 포인트


