with 절과 union 절

김하경·2025년 8월 12일

WITH RECURSIVE hours AS (
SELECT 0 AS h
UNION ALL
SELECT h + 1 FROM hours WHERE h < 23
)
SELECT
hours.h AS hour,
COALESCE(COUNT(a.animal_id), 0) AS adoption_count
FROM hours
LEFT JOIN adoptions a
ON HOUR(a.adopted_at) = hours.h
GROUP BY hours.h
ORDER BY hours.h;

-- 코드를 입력하세요
with recursive hours as
(SELECT 0 as h
union
select h+1 from hours where h<22)
select b.h,
coalesce(count(animal_id),0) as count
from animal_outs a
left join hours b
on b.h=a.hour(datetime)

0개의 댓글