[프로그래머스 ORACLE SQL]자동차 대여 기록 별 대여 금액 구하기★CASE_WHEN_THEN
2023. 6. 23. 13:11
728x90
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/151141
단계별로 풀자
1. CAR_RENTAL_COMPANY_CAR 테이블 ==> CAR_ID CAR_TYPE = 자동차 종류 트럭, DAILY_FEE 뽑기
SELECT CAR_ID, CAR_TYPE , DAILY_FEE FROM CAR_RENTAL_COMPANY_CAR
WHERE CAR_TYPE = '트럭'
2. CAR_RENTAL_COMPANY_RENTAL_HISTORY 테이블 ==> HISTORY_ID, CAR_ID , END_DATE- START_DATE + 1 뽑기
SELECT HISTORY_ID, CAR_ID ,
CASE WHEN (END_DATE- START_DATE + 1) >= 90 THEN '90일 이상'
WHEN (END_DATE- START_DATE + 1) >= 30 THEN '30일 이상'
WHEN (END_DATE- START_DATE + 1) >= 7 THEN '7일 이상'
ELSE '7일 이하'
END AS DURATION
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
3. CAR_RENTAL_COMPANY_DISCOUNT_PLAN 테이블 트럭, DISCOUNT_RATE 뽑기
SELECT CAR_TYPE , DURATION_TYPE , DISCOUNT_RATE FROM CAR_RENTAL_COMPANY_DISCOUNT_PLAN
WHERE CAR_TYPE = '트럭'
4. JOIN 하기_1
SELECT B.HISTORY_ID , B.CAR_ID , B.DURATION, B.DURATION_1 ,A.CAR_TYPE , A.DAILY_FEE FROM
(SELECT CAR_ID, CAR_TYPE , DAILY_FEE FROM CAR_RENTAL_COMPANY_CAR
WHERE CAR_TYPE = '트럭') A LEFT JOIN
(SELECT HISTORY_ID, CAR_ID ,
CASE WHEN (END_DATE- START_DATE + 1) >= 90 THEN '90일 이상'
WHEN (END_DATE- START_DATE + 1) >= 30 THEN '30일 이상'
WHEN (END_DATE- START_DATE + 1) >= 7 THEN '7일 이상'
ELSE '7일 이하'
END AS DURATION,
(END_DATE- START_DATE + 1) AS DURATION_1
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY) B
ON A.CAR_ID = B.CAR_ID
5. JOIN 하기_2
SELECT A.HISTORY_ID , A.CAR_ID , A.DURATION, A.DURATION_1 , A.CAR_TYPE , A.DAILY_FEE ,
CASE WHEN B.DISCOUNT_RATE IS NULL THEN 0
ELSE B.DISCOUNT_RATE END AS DISCOUNT_RATE
FROM
(SELECT B.HISTORY_ID , B.CAR_ID , B.DURATION, B.DURATION_1 ,A.CAR_TYPE , A.DAILY_FEE FROM
(SELECT CAR_ID, CAR_TYPE , DAILY_FEE FROM CAR_RENTAL_COMPANY_CAR
WHERE CAR_TYPE = '트럭') A LEFT JOIN
(SELECT HISTORY_ID, CAR_ID ,
CASE WHEN (END_DATE- START_DATE + 1) >= 90 THEN '90일 이상'
WHEN (END_DATE- START_DATE + 1) >= 30 THEN '30일 이상'
WHEN (END_DATE- START_DATE + 1) >= 7 THEN '7일 이상'
ELSE '7일 이하'
END AS DURATION,
(END_DATE- START_DATE + 1) AS DURATION_1
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY) B
ON A.CAR_ID = B.CAR_ID) A LEFT JOIN
(SELECT CAR_TYPE , DURATION_TYPE , DISCOUNT_RATE FROM CAR_RENTAL_COMPANY_DISCOUNT_PLAN
WHERE CAR_TYPE = '트럭') B
ON A.CAR_TYPE = B.CAR_TYPE AND A.DURATION = B.DURATION_TYPE
6. 최종
SELECT HISTORY_ID , (DAILY_FEE*(1-(DISCOUNT_RATE*0.01))*DURATION_1) AS FEE FROM
(SELECT A.HISTORY_ID , A.CAR_ID , A.DURATION, A.DURATION_1 , A.CAR_TYPE , A.DAILY_FEE ,
CASE WHEN B.DISCOUNT_RATE IS NULL THEN 0
ELSE B.DISCOUNT_RATE END AS DISCOUNT_RATE
FROM
(SELECT B.HISTORY_ID , B.CAR_ID , B.DURATION, B.DURATION_1 ,A.CAR_TYPE , A.DAILY_FEE FROM
(SELECT CAR_ID, CAR_TYPE , DAILY_FEE FROM CAR_RENTAL_COMPANY_CAR
WHERE CAR_TYPE = '트럭') A LEFT JOIN
(SELECT HISTORY_ID, CAR_ID ,
CASE WHEN (END_DATE- START_DATE + 1) >= 90 THEN '90일 이상'
WHEN (END_DATE- START_DATE + 1) >= 30 THEN '30일 이상'
WHEN (END_DATE- START_DATE + 1) >= 7 THEN '7일 이상'
ELSE '7일 이하'
END AS DURATION,
(END_DATE- START_DATE + 1) AS DURATION_1
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY) B
ON A.CAR_ID = B.CAR_ID) A LEFT JOIN
(SELECT CAR_TYPE , DURATION_TYPE , DISCOUNT_RATE FROM CAR_RENTAL_COMPANY_DISCOUNT_PLAN
WHERE CAR_TYPE = '트럭') B
ON A.CAR_TYPE = B.CAR_TYPE AND A.DURATION = B.DURATION_TYPE)
ORDER BY FEE DESC , HISTORY_ID DESC
728x90
반응형
'SQL > String, Date' 카테고리의 다른 글
[프로그래머스 ORACLE SQL]조건에 맞는 사용자 정보 조회하기★SUBSTR()★한국말 열 쓸때 그냥 작은따옴표 붙이지 않기 (0) | 2023.06.23 |
---|---|
[프로그래머스 ORACLE SQL]조회수가 가장 많은 중고거래 게시판의 첨부파일 조회하기★FETCH FIRST 1 ROWS ONLY (0) | 2023.06.23 |
[프로그래머스 ORACLE SQL]자동차 평균 대여 기간 구하기★날짜 뺄때는 1을 더하자! (0) | 2023.06.22 |
[프로그래머스 ORACLE SQL]대여 기록이 존재하는 자동차 리스트 구하기★DISTINCT()중복제거 (0) | 2023.06.22 |
[프로그래머스 ORACLE SQL]조건에 부합하는 중고거래 상태 조회하기★CASE WHEN THEN (0) | 2023.06.22 |