[프로그래머스SQL]

allnight5·2023년 6월 20일
0

프로그래머스SQL

목록 보기
15/19

링크

처음에 해봤던 코드

-- 코드를 입력하세요
SELECT MONTH(START_DATE) AS MONTH, 
CAR_ID, COUNT(HISTORY_ID) AS RECORDS 
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE START_DATE BETWEEN '2022-08-01' AND '2022-10-31'
GROUP BY MONTH, CAR_ID
HAVING COUNT(HISTORY_ID) >= 5
ORDER BY MONTH ASC, CAR_ID DESC;

본 코드

-- 코드를 입력하세요
SELECT MONTH(START_DATE) AS MONTH, CAR_ID, 
COUNT(HISTORY_ID) AS RECORDS 
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE CAR_ID IN(
    SELECT C.CAR_ID 
    FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY AS C
    WHERE C.START_DATE BETWEEN '2022-08-01' AND '2022-10-31'
    GROUP BY C.CAR_ID
    HAVING COUNT(C.CAR_ID) >= 5
)
AND START_DATE BETWEEN '2022-08-01' AND '2022-10-31'
GROUP BY MONTH, CAR_ID
HAVING RECORDS >0
ORDER BY MONTH ASC, CAR_ID DESC

쿼리를 하나 더넣어서 하는 서브쿼리가 아직 미숙하다
좀더 고민하면서 해봐야겠다.

profile
공부기록하기

0개의 댓글