我需要检查LEAD()
函数返回的值是否为NULL。
SELECT LEAD(created_at) OVER (order by id, created_at) - created as diff
答案 0 :(得分:0)
合适的解决方案:
SELECT coalesce(LEAD(created_at) OVER (order by id, created_at), now()) - created as diff
谢谢
答案 1 :(得分:0)
通常,NULL
值用于组中的最后一行。在这种情况下,您可以只使用LEAD()
的3个参数形式:
SELECT (LEAD(created_at, 1, <replacement value>) OVER (order by id, created_at) -
created_at
) as diff
请注意,这只会替换NULL
值,它们是组中的最后一个值。如果数据中有NULL
个值,您实际上将得到NULL
。这通常是您想要的行为。