且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Oracle SQL:使用上一行计算值

更新时间:2022-02-01 17:04:39

请注意,数据库表没有任何固有顺序.假设您可以使用一些ID或其他列来定义行的顺序,则可以使用

Note that database table don't have any intrinsic order. Assuming you have some id or other column you can use to define the rows' order, you could use the lag function to get the t-3 record:

SELECT cur_value, 
       difference, 
       LAG(cur_value, 3, 0) OVER (ORDER BY id) + difference AS new_cur_value
FROM   my_table