3

我有一个数据集,其中包含日期 {yyyy/mm/dd} 和时间 {h,m,s} 和温度 {float} 作为单独的列。

我想通过平均函数汇总每天的温度值。

问题是,我不知道如何自动查询时间属性,例如聚合{h,m, (0-5)s}{h,m, (5-10)s}{h,m, (10-15)s}和...。

4

1 回答 1

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
;

我认为您问题的重要部分是按秒除以范围大小的整数结果进行分组。

于 2012-09-27T13:45:51.003 回答