https://programmers.co.kr/learn/courses/30/lessons/59413
SELECT H.HOUR, IFNULL(OUTS.COUNT,0) AS COUNT
FROM (
SELECT 0 AS HOUR
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12
UNION SELECT 13
UNION SELECT 14
UNION SELECT 15
UNION SELECT 16
UNION SELECT 17
UNION SELECT 18
UNION SELECT 19
UNION SELECT 20
UNION SELECT 21
UNION SELECT 22
UNION SELECT 23 ) H
LEFT JOIN (
SELECT HOUR(DATETIME) AS HOUR, COUNT(*) AS COUNT
FROM ANIMAL_OUTS
GROUP BY HOUR) AS OUTS ON H.HOUR = OUTS.HOUR
다른사람의 풀이
set @hour := -1;
SELECT
(@hour := @hour + 1) as 'HOUR',
(select count(*) from animal_outs where hour(datetime) = @hour) as 'COUNT'
from
animal_outs
where
@hour < 23;
'Algorithm > SQL' 카테고리의 다른 글
[SQL][프로그래머스] 이름이 없는 동물의 아이디 (0) | 2020.06.15 |
---|---|
[SQL][프로그래머스] NULL 처리하기 (0) | 2020.06.15 |
[SQL][프로그래머스] 입양 시각 구하기(1) (0) | 2020.06.15 |
[SQL][프로그래머스] 동명 동물 수 찾기 (0) | 2020.06.15 |
[SQL][프로그래머스] 고양이와 개는 몇 마리 있을까 (0) | 2020.06.15 |