oracle last_value使用过程中的一个细节

测试结果集:
select role_id,update_date from user_info where role_id='6505007898843021313'

使用last_value求出当前role_id的最大的update_date。
select role_id,last_value(update_date)over(partition by role_id order by update_date) from user_info where role_id='6505007898843021313'
输出结果:

发现并没有像我们预期的那样,每个update_date都不一样。
查了一些相关资料,有人指出last_value统计数据的范围默认是: rows between unbounded preceding and current row(从最开始的行到当前行)。
所以需要手动指定last_value统计数据范围为:rows between unbounded preceding and unbounded following(从最开始的行到最后的行)
select role_id,last_value(update_date)over(partition by role_id order by update_date rows between unbounded preceding and unbounded following) from user_info where role_id='6505007898843021313'
返回结果:


为什么first_value没有这个问题呢,思考了一下,first_value本身就是要求从第一行开始,将所有的值都置为第一个值。所以没有这个取数据的范围的问题。

原文地址:https://www.cnblogs.com/gavinYang/p/11197978.html