👉 오늘 한 일
- 프로그래머스 SQL 문제풀이
- 책 편집 & 편집 툴 적용
프로그래머스 SQL
1. 특정 기간동안 대여 가능한 자동차들의 대여비용 구하기 📌
SELECT a.car_id,
a.car_type,
ROUND(a.daily_fee * 30 * (100 - c.discount_rate) / 100, 0) fee
FROM car_rental_company_car a
JOIN car_rental_company_rental_history b
ON a.car_id = b.car_id
JOIN car_rental_company_discount_plan c
ON a.car_type = c.car_type
WHERE a.car_type IN('세단', 'SUV')
AND DATE_FORMAT(b.end_date, "%Y-%m-%d") < "2022-11-01"
OR DATE_FORMAT(b.start_date, "%Y-%m-%d") >= "2022-12-01"
GROUP BY a.car_id, c.duration_type, c.discount_rate
HAVING c.duration_type = "30일 이상"
AND fee >= 500000
AND fee < 2000000
ORDER BY fee DESC, a.car_type, a.car_id DESC
- 다른 사람 풀이
- 11월에 대여하고 있는 모든 car_id를 불러와서 NOT IN 으로 필터링
SELECT
CRCR.CAR_ID,
CRCR.CAR_TYPE,
ROUND(CRCR.DAILY_FEE * 30 * (100 - CRCDP.DISCOUNT_RATE) / 100, 0) AS FEE
FROM
CAR_RENTAL_COMPANY_CAR AS CRCR
INNER JOIN
CAR_RENTAL_COMPANY_DISCOUNT_PLAN AS CRCDP
ON
CRCR.CAR_TYPE = CRCDP.CAR_TYPE
WHERE
CRCR.CAR_ID NOT IN (
SELECT
CAR_ID
FROM
CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE
END_DATE >= '2022-11-01'
AND
START_DATE < '2022-12-01'
)
AND
(
CRCR.CAR_TYPE = '세단'
OR
CRCR.CAR_TYPE = 'SUV'
)
AND
CRCDP.DURATION_TYPE = '30일 이상'
GROUP BY
CRCR.CAR_ID
HAVING
FEE BETWEEN 500000 AND 1999999
ORDER BY
FEE DESC,
CAR_TYPE ASC,
CAR_ID DESC