융무의 기술블로그
article thumbnail

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

 

코딩테스트 연습 - 입양 시각 구하기(2)

ANIMAL_OUTS 테이블은 동물 보호소에서 입양 보낸 동물의 정보를 담은 테이블입니다. ANIMAL_OUTS 테이블 구조는 다음과 같으며, ANIMAL_ID, ANIMAL_TYPE, DATETIME, NAME, SEX_UPON_OUTCOME는 각각 동물의 아이디, 생물

programmers.co.kr

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;

 

profile

융무의 기술블로그

@융무

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!