create table test(Account int, fMonth int, PTDDebit int, YTDDebit int)
insert test select 10001 ,01, 100,null
union all
select
10001 ,02, 150,null
union all
select
10001, 03 ,200,null
union all
select
10002, 01, 200,null
union all
select
10002,02 ,300,null
union all
select
10002, 03, 350 ,null
go
create view view1 as
SELECT a.fmonth, a.account, a.PTDDebit,
SUM(b.PTDDebit)AS RunningTotal
FROM test a
CROSS JOIN test b
WHERE (a.account=b.account and b.fmonth <= a.fmonth)
GROUP BY a.account,a.fmonth,a.PTDDebit
go
update a set a.YTDDebit=b.RunningTotal
FROM test a
join view1 b on a.account= b.account and a.fmonth=b.fmonth
go
drop view view1
go
select*from test
go
drop table test
go