在
from子句中使用子查询:
update table_turnover set closing_balance = (ttprev.prev_closing_balance + ttprev.turnover) from (select tt.*, lag(closing_balance) over (partition by product order by week_number) as prev_closing_balance from table_turnover tt ) ttprev where ttprev.product = tt.product and ttprev.week_number = tt.week_number and week_number > 0;
或者,如果您想在中使用子查询
select:
update table_turnover set closing_balance = (turnover + (select tt2.closing_balance from table_turnover tt2 where tt2.product = tt.product and tt2.week_number = tt.week_number - 1 ) ) where week_number > 0;
为了提高性能(在任何一个版本上),您都希望在上建立索引
table_turnover(product, week_number,closing_balance)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)