【发布时间】:2014-09-07 10:19:01
【问题描述】:
我想知道是否可以在 psql 中获取一组符合此条件的行
表 1/0 23 没有时区的时间戳
让我们想象一组数据
1 12 2014-07-14 09:01:00
1 13 2014-07-14 09:02:00
1 14 2014-07-14 09:03:00
1 15 2014-07-14 09:04:00
0 16 2014-07-14 09:05:00
0 17 2014-07-14 09:06:00
0 18 2014-07-14 09:07:00
1 17 2014-07-14 09:08:00
1 16 2014-07-14 09:09:00
我想按状态检索数据组,但保持日期顺序和状态变化。
即:
1 <avg temp> <avg date> (calculated over the first 4 rows)
0 <avg temp> <avg date> (calculated for the 3 rows with 0)
1 <avg temp> <avg date> (calculated for the last 2 rows)
基本上 1/0 表示加热器开/关,我想 计算平均温度,但按此加热器的状态分组
附言:
显然是一个简单的 select avg(temp),status from log group by status 不会成功,它只会返回 1/0 值,而不是范围和开/关之间的变化
另一个很好处理的情况是保持每个状态更改的第一条记录不变。
即:
1 <temp> <date> (calculated over the first)
1 <avg temp> <avg date> (calculated over the first 3 rows)
0 <temp> <date> (for the row 4)
0 <avg temp> <avg date> (from 5 to 6)
1 <temp> <date> (row 7)
【问题讨论】:
标签: sql postgresql