[PGS] 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기

Organ·2023년 10월 26일
0

[문제 풀이]

목록 보기
107/123

문제

예시


내 풀이

- 2022-08 ~ 2022-10 까지 5회 이상 대여한 차량 아이디
SELECT EXTRACT(MONTH FROM START_DATE), CAR_ID
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
GROUP BY EXTRACT(MONTH FROM START_DATE), CAR_ID
HAVING (EXTRACT(MONTH FROM START_DATE) BETWEEN 8 AND 10)
AND COUNT(*) >= 5

- 2022-08 ~ 2022-10 까지 월별 차량 대여 횟수
SELECT EXTRACT(MONTH FROM START_DATE), CAR_ID, COUNT(*)
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
GROUP BY EXTRACT(MONTH FROM START_DATE), CAR_ID
WHERE CAR_ID = 총대여 횟수 5 이상

SELECT EXTRACT(MONTH FROM START_DATE) MONTH, CAR_ID, COUNT(*) RECORDS
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE CAR_ID IN (
    SELECT CAR_ID
    FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
    WHERE EXTRACT(MONTH FROM START_DATE) BETWEEN 8 AND 10
    GROUP BY CAR_ID
    HAVING COUNT(*) >= 5
) AND 8 <= EXTRACT(MONTH FROM START_DATE) AND EXTRACT(MONTH FROM START_DATE) < 11
GROUP BY EXTRACT(MONTH FROM START_DATE), CAR_ID
HAVING COUNT(*) <> 0
ORDER BY MONTH, CAR_ID DESC;

0개의 댓글