๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Welcome.
:: DBMS ๐Ÿšฉ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  kit ํ’€์ด

GROUP BY>๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ์ž๋™์ฐจ๋“ค์˜ ์›”๋ณ„ ๋Œ€์—ฌ ํšŸ์ˆ˜ ๊ตฌํ•˜๊ธฐ

by EunBird 2023. 2. 26.

mysql

-- 
SELECT DATE_FORMAT(START_DATE, '%m') AS MONTH, CAR_ID, COUNT(HISTORY_ID) AS RECORDS
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE 
    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, CAR_ID
HAVING RECORDS >= 0
ORDER BY MONTH, 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 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
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 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(*) >= 5
        )
GROUP BY MONTH, CAR_ID
HAVING RECORDS > 0
ORDER BY MONTH, CAR_ID DESC;

 

 

 

728x90

๋Œ“๊ธ€