PostgreSQL中具有条件的连续记录

bxjv4tth  于 2023-02-15  发布在  PostgreSQL
关注(0)|答案(1)|浏览(150)
Create table  weather_forecast (
date date,
temperature decimal,
avg_humidity decimal,
avg_dewpoint decimal, 
avg_barometer decimal,
avg_windspeed decimal,
avg_gutspeed decimal,
avg_direction decimal
, rainfall_month decimal
, rainfall_year decimal
, maxrain_permin decimal
, max_temp decimal
, min_temp decimal
, max_humidity decimal,
min_humidity decimal
, max_pressure decimal
, min_pressure decimal
, max_winspeed decimal
, max_gutspeed decimal
, maxheat_index decimal
, month int
, diff_pressure decimal(7,5) 
);

如果最大阵风速度从每小时55英里增加,我需要获取未来4天的详细资料。我只能做到这一点:

with t1 as(
select max_gutspeed, date
from weather_forecast
where max_gutspeed >55 )
, t2 as (
    select date, row_number() over () as rn
    from weather_forecast)
 select distinct(rn), t1.date, t1.max_gutspeed
from t1
inner join t2 on t1.date = t2.date
order by t2.rn asc`

另外,我如何找到温度下降的最大和最小天数?
我试着用临时表做,但我卡住了,不知道下一步。

zzlelutf

zzlelutf1#

你可以用你的date构造一个daterange,然后再构造一个4天的interval,然后列出所有在ANY中包含<@的“日期”上的东西。

select * 
from weather_forecast t
where date <@ ANY(
   select daterange(date, (date+'4 days'::interval)::date, '[]')
   from weather_forecast
   where max_gutspeed >55 )
order by t.rn asc;

相关问题