본문 바로가기

Coding Test/프로그래머스

[프로그래머스/MySQL] 자동차 대여 기록 별 대여 금액 구하기

SELECT H.HISTORY_ID, ROUND(C.DAILY_FEE * (DATEDIFF(H.END_DATE, H.START_DATE) + 1) * (1 - IFNULL(DISCOUNT_RATE, '0') / 100)) AS FEE
    FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY AS H 
        INNER JOIN CAR_RENTAL_COMPANY_CAR AS C
            ON H.CAR_ID = C.CAR_ID AND C.CAR_TYPE = '트럭'
        LEFT JOIN CAR_RENTAL_COMPANY_DISCOUNT_PLAN AS P
            ON C.CAR_TYPE = P.CAR_TYPE AND P.DURATION_TYPE = (
                CASE WHEN (DATEDIFF(H.END_DATE, H.START_DATE) + 1) BETWEEN 7 AND 29 THEN '7일 이상'
                    WHEN (DATEDIFF(H.END_DATE, H.START_DATE) + 1) BETWEEN 30 AND 89 THEN '30일 이상'
                    WHEN (DATEDIFF(H.END_DATE, H.START_DATE) + 1) >= 90 THEN '90일 이상'
                    ELSE '' END
            )
    ORDER BY FEE DESC, H.HISTORY_ID DESC;