gpt4 book ai didi

sql - 为什么 Last_value 需要 frame_clause 才能获得像 First_Value - Window Function PostgreSQL 这样的结果

转载 作者:行者123 更新时间:2023-12-05 00:55:47 26 4
gpt4 key购买 nike

这段代码展示了 3 次尝试以及为什么 Last_value 需要 frame_clause 才能获得像 First_Value 这样的结果

select timestamp, 
last_value(timestamp) over (partition by campaign_id order by timestamp
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as last_day,
last_value(timestamp) over (partition by campaign_id order by timestamp) as last_day2,
first_value(timestamp) over (partition by campaign_id order by timestamp desc) as last_day3
from t1

Frame_clause 是

RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

结果:

enter image description here

最佳答案

从技术上讲,所有窗口定义都应该有一个RANGE

习惯是如果没有指定RANGE,则假定为`RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW'。

使用默认的 RANGElast_value() 始终是 CURRENT ROW,因此必须指定 UNBOUNDED FOLLOWING得到你想要的。

关于sql - 为什么 Last_value 需要 frame_clause 才能获得像 First_Value - Window Function PostgreSQL 这样的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63517830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com