티스토리 뷰

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

 

프로그래머스

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

programmers.co.kr

with DISCOUNT_PLAN as (
    select 
        CAR_TYPE
        , REGEXP_SUBSTR(DURATION_TYPE, '[0-9]+') as 'duration' 
        , DISCOUNT_RATE
    from CAR_RENTAL_COMPANY_DISCOUNT_PLAN
)
, RENTAL_HISTORY as (
select 
    HISTORY_ID, CAR_ID
    , datediff(END_DATE, START_DATE) + 1 as 'duration'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
)
, total_rental as (
select
    b.history_id
    , max(if(c.discount_rate is null, 0, c.discount_rate))as discount_rate
From CAR_RENTAL_COMPANY_CAR as a
join RENTAL_HISTORY as b on a.CAR_ID = b.CAR_ID
left join DISCOUNT_PLAN as c on a.CAR_TYPE = c.CAR_TYPE
    and b.duration >= c.duration
group by 1
)
SELECT 
    distinct b.HISTORY_ID
    , floor(a.DAILY_FEE*(1-c.DISCOUNT_RATE/100)*b.duration) as FEE
From CAR_RENTAL_COMPANY_CAR as a
join RENTAL_HISTORY as b on a.CAR_ID = b.CAR_ID
join total_rental as c on b.history_id = c.history_id
where a.CAR_TYPE = '트럭'
order by 2 desc, 1 desc
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함