-- https://school.programmers.co.kr/learn/courses/30/lessons/157339
SELECT C.CAR_ID, C.CAR_TYPE, ROUND(30*C.DAILY_FEE*(100-CP.DISCOUNT_RATE)/100) AS FEE
FROM (SELECT CAR_ID, CAR_TYPE, DAILY_FEE FROM CAR_RENTAL_COMPANY_CAR WHERE CAR_TYPE IN ('세단','SUV')) AS C
JOIN (SELECT CAR_TYPE, DISCOUNT_RATE FROM CAR_RENTAL_COMPANY_DISCOUNT_PLAN WHERE DURATION_TYPE ='30일 이상'AND CAR_TYPE IN ('SUV','세단')) AS CP
ON C.CAR_TYPE=CP.CAR_TYPE
WHERE C.CAR_ID NOT IN (SELECT DISTINCT(CAR_ID)
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE ((DATE_FORMAT(START_DATE, '%Y-%m-%d') BETWEEN '2022-11-01' AND '2022-11-30')
OR (DATE_FORMAT(END_DATE, '%Y-%m-%d') BETWEEN '2022-11-01' AND '2022-11-29')))
AND ((30*C.DAILY_FEE*(100-CP.DISCOUNT_RATE)/100 >= 500000) AND (30*C.DAILY_FEE*(100-CP.DISCOUNT_RATE)/100 < 2000000))
ORDER BY FEE DESC, C.CAR_TYPE ASC, C.CAR_ID DESC
왜 틀렸는지 모르겠다..
어떤 게 걸려서 틀렸다고 하는거지..?
데이터를 확인해보니까 시작일과 종료일이 11월 안에 포함되지 않더라도 11월을 더 큰 범위로 포함하는 예외적인 경우가 있었다 역시 데이터를 확인하는 걸 습관을 들여야하는데 맨날 귀찮아서 안하다가 이런 천벌을 받는듯...
SELECT C.CAR_ID, C.CAR_TYPE, ROUND(30*C.DAILY_FEE*(100-CP.DISCOUNT_RATE)/100) AS FEE
FROM (SELECT CAR_ID, CAR_TYPE, DAILY_FEE FROM CAR_RENTAL_COMPANY_CAR WHERE CAR_TYPE IN ('세단','SUV')) AS C
JOIN (SELECT CAR_TYPE, DISCOUNT_RATE FROM CAR_RENTAL_COMPANY_DISCOUNT_PLAN WHERE DURATION_TYPE ='30일 이상'AND CAR_TYPE IN ('SUV','세단')) AS CP
ON C.CAR_TYPE=CP.CAR_TYPE
WHERE C.CAR_ID IN (SELECT CAR_ID
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
GROUP BY CAR_ID
HAVING MAX(START_DATE) > '2022-11-30' OR MAX(END_DATE) <'2022-11-01')
AND ((30*C.DAILY_FEE*(100-CP.DISCOUNT_RATE)/100 >= 500000) AND (30*C.DAILY_FEE*(100-CP.DISCOUNT_RATE)/100 < 2000000))
ORDER BY FEE DESC, C.CAR_TYPE ASC, C.CAR_ID DESC
만약 2월이나 3월처럼 상반기 달을 찾는 거였으면 max를 쓸 수 없어서 좋은 풀이는 아니었던 것 같다
ROUND
대신 FLOOR
을 써도 OKSELECT
문의 칼럼에 대한 조건을 쓸 때 HAVING
을 쓸 수 있다 -> FEE계산 두번 안써도 됨HAVING
에서 사용하는 칼럼은 SELECT
에 있어야한다NOT IN
을 사용할 경우 범위⭐⭐⭐NOT IN (
SELECT CAR_ID
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE END_DATE >= '2022-11-01' AND START_DATE <= '2022-11-30'
)