考虑下表:
create table measurement (
datetime timestamp,
temperature numeric(5,2)
);
我想在SQL
中创建一个PostgreSQL
- 查询,该查询提取温度高于50°C的行至少30分钟,理想情况下从何时开始知道温度是多少实际上高于50°C。示例数据如下:
datetime temperature
------------------- -----------
2017-03-15 19:00:10 49.56
2017-03-15 19:15:10 52.81
2017-03-15 19:30:10 49.00
2017-03-15 19:45:10 52.88
2017-03-15 20:00:10 49.56
2017-03-15 20:15:10 49.13
2017-03-15 20:30:10 51.31 <--
2017-03-15 20:45:10 52.06 <--
2017-03-15 21:00:10 50.50 <--
2017-03-15 21:15:10 50.50 <--
2017-03-15 21:30:10 49.38
2017-03-15 21:45:10 47.44
2017-03-15 22:00:10 46.19
2017-03-15 22:15:10 45.44
2017-03-15 22:30:10 50.25
2017-03-15 22:45:10 48.56
2017-03-15 23:00:10 51.25 <--
2017-03-15 23:15:10 50.44 <--
2017-03-15 23:30:10 50.63 <--
2017-03-15 23:45:10 46.75
答案 0 :(得分:5)
因此,温度高于50的第一个身份组。这是一个缺口和岛屿问题。然后,您可以汇总岛屿以获取所需信息:
class Post(ndb.Model):
name = ndb.StringProperty()
votes = ndb.IntegerPropery()
答案 1 :(得分:2)
Gordon的解决方案可以简化为单个OLAP功能:
select min(datetime), max(datetime), count(*) as numrecs, avg(temperature)
from
(
select datetime, temperature,
-- previous time when temperature was < 50
-- same time for all rows with a temp >= 50
max(case when temperature < 50 then datetime end)
over (order by datetime
rows unbounded preceding) as prevlow
from measurement
) as dt
where temperature >= 50
group by prevlow
having max(datetime) >= min(datetime) + interval '30' minute;