如何使用postgresql在滞后函数中应用分区

How to apply partition by in lag function using postrgresql

我有一个table如下图

subject_id,    date_inside,             value
1           2110-02-12 19:41:00          1.3
1           2110-02-15 01:40:00          1.4
1           2110-02-15 02:40:00          1.5
2           2110-04-15 04:07:00          1.6
2           2110-04-15 08:00:00          1.7
2           2110-04-15 18:30:00          1.8

我想计算 date difference between consecutive rows for each subject

我尝试了以下

select a.subject_id,a.date_inside, a.value,
a. date_inside- lag(a. date_inside) over (order by a. date_inside) as difference
from table1 a

虽然上述方法有效,但我无法为每个科目申请 partition by。因此,它最终会计算所有行的差异(不考虑 subject_id)。基本上,每个主题的最后一行必须是 null,因为那是他或她的最后一行(不应从下一个主题的连续记录中减去)

我希望我的输出如下所示

subject_id,    date_inside,             difference
1           2110-02-12 19:41:00          66 hours
1           2110-02-15 01:40:00          1 hour 
1           2110-02-15 02:40:00          NULL
2           2110-04-15 04:07:00          3 hours, 53 minutes 
2           2110-04-15 08:00:00          10 hours, 30 minutes
2           2110-04-15 18:30:00          NULL

将“分区依据”想象成类似于您可以使用“分组依据”的方式。在这种情况下,逻辑边界由 subject_id 确定,因此只需将其作为 over 子句的一部分包含:

select a.subject_id,a.date_inside, a.value,
a.date_inside - lag(a.date_inside) over (partition by a.subject_id order by a.date_inside) as difference
from table1

只需添加一个 PARTITION BY 子句,而且您的预期输出似乎需要 LEAD,而不是 LAG:

SELECT subject_id, date_inside, value,
       LEAD(date_inside) OVER (PARTITION BY subject_id ORDER BY date_inside)
           - date_inside AS difference
FROM table1
ORDER BY
    subject_id,
    date_inside;