Anda perlu menghasilkan jam. Berikut adalah salah satu metode menggunakan generate_series()
:
select '00:00'::time + g.h * interval '1 hour',
count(order_id) as orders
from generate_series(0, 23, 1) g(h) left join
order_table ot
on extract(hour from create_date) = g.h and
date_trunc('day', create_date) = '2013-05-09'
group by g.h
order by g.h;
Atau sebagai alternatif:
select g.dte, count(order_id) as orders
from generate_series('2013-05-09'::timestamp, '2013-05-09 23:00:00'::timestamp, interval '1 hour') g(dte) left join
order_table ot
on g.dte = date_trunc('hour', create_date)
group by g.dte
order by g.dte;