Postgres:每分钟数据的聚合平均值

时间:2014-02-06 15:51:40

标签: postgresql

我目前有一个查询,它返回日期时间和表中的值。

Select time, value from table;

2014-02-05 15:49:54.458 70
2014-02-05 15:49:55.46  70
2014-02-05 15:49:56.483 70
2014-02-05 15:49:57.487 70
2014-02-05 15:49:58.503 70
2014-02-05 15:50:00.042 70
2014-02-05 15:50:01.546 70
2014-02-05 15:50:03.056 70
2014-02-05 15:50:04.093 70

我希望做的是像

select average(value) from table group by (cast time to min resolution)time;

2014-02-05 15:49    70
2014-02-05 15:50    70

1 个答案:

答案 0 :(得分:2)

这应该这样做:

select date_trunc('minute', time), 
       avg(value)
from the_table
group by date_trunc('minute', time)
order by date_trunc('minute', time);

手册中的更多详细信息:http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC


顺便说一下:timevalue是列的可怕名称