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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

풀이

 

group by 로 간단하게 묶으면 될 것같지만, 조건에는 집계 결과가 0인 시간대도 출력해야 함

이를 위해 with recursive 재귀 쿼리를 사용

0 부터 1까지의 시간대별 데이터를 가진 hour 테이블을 animal_outs 테이블과 outer join

 

 

with recursive hourrange AS (
  select 0 AS hour
  union ALL
  select hour + 1
  from hourrange
  where hour < 23
)

select h.hour, count(animal_id)
from hourrange as h left outer join animal_outs as a
on h.hour = hour(a.datetime)
group by h.hour
order by h.hour;

+ Recent posts