如果有一个表示次序的字段,比如ID,就可以
select (select sum(b) from tablename where id <a.id) as a,b,(select sum(b) from tablename where id <=a.id) as a as c from tablename
order by id
select (select sum(b) from tablename where id <a.id) as a,b,(select sum(b) from tablename where id <=a.id) as a as c from tablename
order by id
Create table #tmp (A int,B int ,C int)
INSERT INTO #tmp values (1,5,NULL)
INSERT INTO #tmp values (NULL,2,NULL)
INSERT INTO #tmp values (NULL,5,NULL)
INSERT INTO #tmp values (NULL,98,NULL)
GODeclare @val1 int ,@val2 int
set @val2=0
UPDATE #tmp SET @val1=A=ISNULL(C,0),@val2=C=@val2+B+ISNULL(A,0)
UPDATE #tmp SET A=C-B select * From #tmp --結果Drop table #tmp --殺掉測試資料
UPDATE #tmp SET @val2=C=@val2+B +ISNULL(A,0)
update #tmp set A=C-B