我想通过时间戳进行移动平均.

数据样本:

ID   Timestamps          Temperature
1    2007-09-14 22:56:12 5.39
2    2007-09-14 22:58:12 5.34
3    2007-09-14 23:00:12 5.16
4    2007-09-14 23:02:12 5.54
5    2007-09-14 23:04:12 5.30
6    2007-09-14 23:06:12 5.20
7    2007-09-14 23:10:12 5.39
8    2007-09-14 23:12:12 5.34
9    2007-09-14 23:20:12 5.16
10   2007-09-14 23:24:12 5.54
11   2007-09-14 23:30:12 5.30
12   2007-09-14 23:33:12 5.20
13   2007-09-14 23:40:12 5.39
14   2007-09-14 23:42:12 5.34
15   2007-09-14 23:44:12 5.16
16   2007-09-14 23:50:12 5.54
17   2007-09-14 23:52:12 5.30
18   2007-09-14 23:57:12 5.20

主要挑战:

我如何学习代码,以便在由于采样频率不同而没有精确的15分钟时间间隔的情况下,每15分钟进行一次区分.

推荐答案

假设您希望在每15分钟间隔后重新启动滚动平均值:

select id, 
       temp,
       avg(temp) over (partition by group_nr order by time_read) as rolling_avg
from (       
  select id, 
         temp,
         time_read, 
         interval_group,
         id - row_number() over (partition by interval_group order by time_read) as group_nr
  from (
    select id, 
           time_read, 
           'epoch'::timestamp + '900 seconds'::interval * (extract(epoch from time_read)::int4 / 900) as interval_group,
           temp
    from readings
  ) t1
) t2
order by time_read;

它以Depesz's solution为基础,按"时间范围"分组:

下面是一个SQLFiddle示例:http://sqlfiddle.com/#!1/0f3f0/2

Postgresql相关问答推荐

利用PostgreSQL查询中表的顺序来计算包含每次时间的时间范围

使用regexp获取表名

PostgreSQL散列连接与嵌套循环和散列索引

为什么我的应用程序接收的是空值而不是布尔值?

让Docker Compose Container等待容器PostgreSQL并恢复

无法将 json 范围读取为 pgtype.Int4range

这个 Supbase 查询在 SQL ( Postgresql ) 中的类似功能是什么

订阅标签保存在哪个表中?

如何使用 SQLAlchemy 将列默认设置为 PostgreSQL 函数?

PostgreSQL:在timestamp::DATE 上创建索引

PostgreSQL 中基于时间戳的移动平均线

带有 -C 选项的 pg_restore 不会创建数据库

如何使用字符串作为 PostgreSQL 咨询锁的键?

H2 postgresql mode模式似乎不起作用

在 PostgreSQL 中显示正在运行的查询的全文

防止 CHARACTER VARYING 字段中出现空字符串

postgres regexp_replace 只想允许 a-z 和 A-Z

我应该在 Django DATABASE ENGINE 中使用哪个 Postgres 值?

Heroku 上的 Postgres 并将单个表转储到转储文件

将postgres中的所有记录转换为Titlecase,首字母大写