[Programmers SQL 고득점 Kit] GROUP BY [4/4] 입양 시각 구하기(2)

문제 link
https://programmers.co.kr/learn/courses/30/lessons/59413

Code link
https://github.com/JamesJung01/programmers_sql_kit/blob/main/03_GROUP_BY/GROUP_BY-4.sql

SET @ROW_NUM = -1;
SELECT HOUR, MAX(COUNT) AS COUNT
FROM (
    (SELECT HOUR(DATETIME) AS HOUR, COUNT(*) AS COUNT
     FROM ANIMAL_OUTS
     GROUP BY HOUR)
     UNION
    (SELECT @ROW_NUM := @ROW_NUM + 1 AS HOUR, 0 AS COUNT
     FROM ANIMAL_OUTS
     LIMIT 24)
    ) AS RESULT_SET
GROUP BY HOUR
ORDER BY HOUR;

좋은 웹페이지 즐겨찾기