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

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

使用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'
输出结果:

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

发现并没有像我们预期的那样,每个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'
返回结果:

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

相关文章:

  • 2022-12-23
  • 2021-06-09
  • 2021-05-23
  • 2021-09-07
  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2022-01-26
猜你喜欢
  • 2021-10-31
  • 2021-06-20
  • 2021-12-14
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案