select
date_trunc('hour', clock) "hour",
sum((itemid = 23661)::integer) hits
from history_uint
group by 1
order by 1
Or with all the hours filled:
select
s.hour, count(itemid = 23661 or null) hits
from
(
select date_trunc('hour', clock) "hour", itemid
from history_uint
) h
right join (
select date_trunc('hour', d) "hour"
from generate_series (
(select min(clock::date)),
(select max(clock)::date + 1) - interval '1 hour',
'1 hour'
) s(d)
) s on s.hour = h.hour
group by 1
order by 1
source
share