Resample on time series data

偶尔善良 提交于 2019-12-11 02:45:47

问题


I have a table with time series column in the millisecond, I want to resample the time series and apply mean on the group. How can I implement it in Postgres?

"Resample" means aggregate all time stamps within one second or one minute. All rows within one second or one minute form a group.

table structure

date    x    y    z

回答1:


Use date_trunc() to truncate timestamps to a given unit of time, and GROUP BY that expression:

SELECT date_trunc('minute', date) AS date_truncated_to_minute
      ,avg(x) AS avg_x
      ,avg(y) AS avg_y
      ,avg(z) AS avg_z
FROM   tbl
GROUP  BY 1;

Assuming your misleadingly named date column is actually of type timestamp.

Related answer with more details and links:

  • PostgreSQL: running count of rows for a query 'by minute'


来源:https://stackoverflow.com/questions/26465077/resample-on-time-series-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!