Data Analyst KIM

[MySQL] 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기(프로그래머스) 본문

데이터 분석/Coding Test

[MySQL] 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기(프로그래머스)

김두연 2023. 11. 3. 08:49
반응형

SELECT MONTH(START_DATE) AS MONTH
     , CAR_ID
     , COUNT(*) AS RECORDS
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE DATE_FORMAT(START_DATE,'%Y-%m') BETWEEN '2022-08' AND '2022-10'
      AND CAR_ID IN (SELECT CAR_ID
                     FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
                     WHERE DATE_FORMAT(START_DATE,'%Y-%m') BETWEEN '2022-08' AND '2022-10'
                     GROUP BY CAR_ID
                     HAVING COUNT(CAR_ID) >= 5) 
GROUP BY MONTH(START_DATE), CAR_ID 
HAVING RECORDS > 0 
ORDER BY MONTH, CAR_ID DESC
반응형