具有相同的站号(参考号)的记录,如何将记录与2小时前的记录进行比较

时间:2019-02-08 05:04:38

标签: sql-server

我有许多测量站的时间序列数据和发达的方法,下面将进行描述,以确定河流水位是上升,稳定还是下降。

方法: 在一个时间范围(例如两个小时)内获取最旧的值,并使用每个公差范围(例如0.5厘米)内的公差方法将该值与最新值进行比较,则该位置“稳定”。如果最新值高于公差,则趋势为“上升”,如果低于最新值,则为“下降”。

如何将方法转移到#T-SQL?

日期设置格式类似于网站,价值和DT

Site     Value       DT
12345678    0   13/01/2019 0:00
12345678    0   13/01/2019 1:00
12345678    0   13/01/2019 2:00
12345678    0   13/01/2019 3:00
12345678    0   13/01/2019 4:00
12345678    0   13/01/2019 5:00
12345678    0   13/01/2019 6:00
12345678    0   13/01/2019 7:00
12345678    0   13/01/2019 8:00
12345678    0   13/01/2019 9:00
12345678    0   13/01/2019 10:00
12345678    0   13/01/2019 11:00
12345678    0   13/01/2019 13:00
12345678    0   13/01/2019 14:00
12345678    0   13/01/2019 16:00
12345678    0   13/01/2019 17:00
12345678   0.05 13/01/2019 17:57     
12345678   0.15 13/01/2019 17:57     
12345678   0.20 13/01/2019 17:58     
12345678   0.25 13/01/2019 17:58     
12345678   0.30 13/01/2019 17:59     
12345678   0.35 13/01/2019 17:59     
12345678   0.40 13/01/2019 18:00            
12345678   0.50 13/01/2019 18:01     
12345678   0.55 13/01/2019 18:03     
12345678   0.40 13/01/2019 19:00     
12345678   0.40 13/01/2019 20:00     
12345678   0.20 13/01/2019 21:00     
12345678   0.10 13/01/2019 22:00          
12345678    0   13/01/2019 23:00
12345678    0   14/01/2019 0:00
12345678    0   14/01/2019 1:00
12345678    0   14/01/2019 2:00
12345678    0   14/01/2019 3:00
12345678    0   14/01/2019 4:00
12345678    0   14/01/2019 5:00
12345678    0   14/01/2019 6:00
12345678    0   14/01/2019 7:00
12345678    0   14/01/2019 8:00
12345678    0   14/01/2019 9:00
12345678    0   14/01/2019 10:00
12345678    0   14/01/2019 11:00
12345678    0   14/01/2019 12:00
12345678    0   14/01/2019 13:00
12345678    0   14/01/2019 14:00
12345678    0   14/01/2019 15:00
12345678    0   14/01/2019 16:00
12345678    0   14/01/2019 17:00
12345678    0   14/01/2019 18:00
12345678    0   14/01/2019 19:00
12345678    0   14/01/2019 20:00
12345678    0   14/01/2019 21:00
12345678    0   14/01/2019 22:00

我尝试使用DATEADDBETWEEN在两个小时内找到最旧的值,但没有成功。

这里是预期的输出数据集(手动生成)。之所以趋势在13/01/2019 17:57上升是因为,首先它将两个小时的时间范围内的最旧值(即13/01/2019 16:00和0的值)与自身进行比较(0.5),也超过了0.5的公差(0.5-0 = 0.5)。这两个因素导致了上升趋势的结果。在13/01/2019 20:00,该值达到0.40,与两个小时前(18:00)相同,并且相差小于0.5,从而稳定。在13/01/2019 21:00,趋势正在下降,因为该值与19:00(0.4)的值之间的比较加上相差大于0.5。

Site     Value       DT              Tendency
12345678    0   13/01/2019 0:00      Steady
12345678    0   13/01/2019 1:00      Steady
12345678    0   13/01/2019 2:00      Steady
12345678    0   13/01/2019 3:00      Steady
12345678    0   13/01/2019 4:00      Steady
12345678    0   13/01/2019 5:00      Steady
12345678    0   13/01/2019 6:00      Steady
12345678    0   13/01/2019 7:00      Steady
12345678    0   13/01/2019 8:00      Steady
12345678    0   13/01/2019 9:00      Steady
12345678    0   13/01/2019 10:00     Steady
12345678    0   13/01/2019 11:00     Steady
12345678    0   13/01/2019 13:00     Steady
12345678    0   13/01/2019 14:00     Steady
12345678    0   13/01/2019 16:00     Steady
12345678    0   13/01/2019 17:00     Steady
12345678   0.05 13/01/2019 17:57     Rising
12345678   0.15 13/01/2019 17:57     Rising
12345678   0.20 13/01/2019 17:58     Rising
12345678   0.25 13/01/2019 17:58     Rising
12345678   0.30 13/01/2019 17:59     Rising
12345678   0.35 13/01/2019 17:59     Rising
12345678   0.40 13/01/2019 18:00     Rising
12345678   0.50 13/01/2019 18:01     Rising
12345678   0.55 13/01/2019 18:03     Rising
12345678   0.40 13/01/2019 19:00     Rising
12345678   0.40 13/01/2019 20:00     Steady
12345678   0.20 13/01/2019 21:00     Falling
12345678   0.10 13/01/2019 22:00     Falling 

1 个答案:

答案 0 :(得分:0)

您可以使用row_number交替使用以获得第一个和最后一个值:

declare @Now datetime = getdate()

select 
    CurrentValue = max(iif(DescRID = 1, Value, null)),
    PastValue =  max(iif(AscRID = 1, Value, null))
from 
(
    select 
        AscRID = row_number() over( order by Dt),
        DescRID = row_number() over (order by Dt desc),
        Value
    from #data
    where Dt >= dateadd(hour, -2, @Now)
) a
where AscRID = 1 
    or DescRID = 1