我有一个数据集,其中包含日期{yyyy / mm / dd}和时间{h,m,s}和温度{float}作为单个列。
我希望按平均函数汇总每一天的温度值。
问题是,我不知道如何查询时间属性来自动说出汇总{h,m, (0-5)s}
和{h,m, (5-10)s}
以及{h,m, (10-15)s}
和...。 / p>
答案 0 :(得分:2)
select
day,
to_char(date_trunc('minute', "time"), 'HH24:MI') as "minute",
extract(second from "time")::integer / 5 as "range",
avg(temperature) as average
from (
select d::date as day, d::time as "time", random() * 100 as temperature
from generate_series('2012-01-01', '2012-01-03', '1 second'::interval) s(d)
) d
group by 1, 2, 3
order by 1, 2, 3
;
如果你想要所有日子的平均值:
select
to_char(date_trunc('minute', "time"), 'HH24:MI') as "minute",
extract(second from "time")::integer / 5 as "range",
avg(temperature) as average
from (
select d::time as "time", random() * 100 as temperature
from generate_series('2012-01-01', '2012-01-03', '1 second'::interval) s(d)
) d
group by 1, 2
order by 1, 2
;
我认为你问题的重要部分是将秒的除法的整数结果除以范围大小。