Ваш оператор обновления просто суммирует все строки перед текущим порядковым номером.
create table tbl (FiscalYear int, OrderDate date, OrderNumber int, SubTotal decimal(10,2), RunningTotal decimal(10,2));
insert into tbl values
(2011, '20110531', 1, 5000.02, null),
(2011, '20110531', 2, 1000.15, null),
(2011, '20110531', 3, 700.25, null),
(2011, '20110531', 4, 225.02, null),
(2011, '20110531', 5, 1258.25, null),
(2011, '20110531', 6, 1000.00, null),
(2011, '20110531', 7, 695.20, null),
(2011, '20110531', 8, 789.25, null),
(2011, '20110531', 9, 2125.02, null);
GO
Запрос CTE вычисляет совокупные итоги и секундыобновите таблицу.
;with x as
(
select FiscalYear,
OrderDate,
OrderNumber,
SubTotal,
sum(SubTotal) over (partition by FiscalYear
order by FiscalYear, OrderDate, OrderNumber) as CumTotal
from tbl
)
update t
set RunningTotal = CumTotal
from tbl t
join x
on x.Fiscalyear = t.FiscalYear
and x.OrderDate = t.OrderDate
and x.OrderNumber = t.OrderNumber;
GO
9 rows affected