问题描述
在Postgres SQL
中,当前的“问题重置”和下一个停止时间之间有区别。我不明白,如何使用窗口功能完成此操作。我尝试了NEXT_VALUE
和FIRST_VALUE
,但是我看到了移动聚合的示例。我只需要一个查询就可以做到这一点。
我需要实现'22/08/2020 11:29:00'
和'17/08/2020 11:19:00'
之间的差异,从而告诉我运行时间的长短。
解决方法
您似乎想要每个stoptime
与最近的issue
之间的区别。如注释中所述,如果问题和停止时间正确地交织在一起,则可以如下使用窗口函数:
select t.*,stop_time - max_issue as diff
from (
select t.*,max(issue) over(order by issue) max_issue
from mytable t
) t
where stop_time is not null