我在Zabbix数据库中有值,每分钟加载到它上面。我需要一个计算不同日期的小时总计的查询,例如2013年3月20日,我有一行0:00,其中包含时间> = 0:00和<凌晨1点等。然后我会在上午1点到凌晨2点之间有另一行。我正在使用下面的查询,但我必须不断改变时间。我正在寻找的是一个查询,它将为我生成一天中每小时的24行。请帮忙。
SELECT
SUM(CASE WHEN itemid = 23661
THEN value ELSE 0 END) Hits
FROM history_uint WHERE
clock >= EXTRACT(EPOCH FROM TIMESTAMP '2013-03-24 00:00:00')
AND clock < EXTRACT(EPOCH FROM TIMESTAMP '2013-03-24 01:00:00')
答案 0 :(得分:1)
您是否尝试按小时分组结果?
SELECT DATEPART(HOUR, timestamp) [HOUR]
, SUM(CASE WHEN itemid = 23661 THEN value ELSE 0 END) hits
FROM history_uint
WHERE clock >= EXTRACT(EPOCH FROM timestamp '2013-03-24')
AND clock < EXTRACT(EPOCH FROM timestamp '2013-03-25')
GROUP BY DATEPART(HOUR, timestamp);
答案 1 :(得分:0)
类似的东西:
select '2013-03-20'::date + delta * '1 hour'::interval
from generate_series(0,23) g(delta)
答案 2 :(得分:0)
select
date_trunc('hour', clock) "hour",
sum((itemid = 23661)::integer) hits
from history_uint
group by 1
order by 1
或者填满所有时间:
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