在SQL中计算当前行与下一个可用日期值之间的差异

问题描述

Postgres SQL中,当前的“问题重置”和下一个停止时间之间有区别。我不明白,如何使用窗口功能完成此操作。我尝试了NEXT_VALUEFIRST_VALUE,但是我看到了移动聚合的示例。我只需要一个查询就可以做到这一点。

我需要实现'22/08/2020 11:29:00''17/08/2020 11:19:00'之间的差异,从而告诉我运行时间的长短。

enter image description here

解决方法

您似乎想要每个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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...