declare @T table([id] int,[r1] int,[r2] int) Insert @T select 1,5,0 union all select 2,0,2 union all select 3,0,1 union all select 4,0,1 union all select 5,0,1
Select *,(select sum([r1]-[r2]) from @T where [id]<=t.[id])r3 from @T t(5 個資料列受到影響) id r1 r2 r3 ----------- ----------- ----------- ----------- 1 5 0 5 2 0 2 3 3 0 1 2 4 0 1 1 5 0 1 0(5 個資料列受到影響)
declare @t1 table(id int, sr numeric(12,2),zc numeric(12,2)) insert into @t1 select 1,5,0 union all select 2,0,2 union all select 3,0,1 union all select 4,0,1 union all select 5,0,1 SELECT id,sr,zc,(select sum(a.sr-a.zc) from @t1 a where a.id<=b.id) FROM @t1 b(5 行受影响) id sr zc ----------- --------------------------------------- --------------------------------------- --------------------------------------- 1 5.00 0.00 5.00 2 0.00 2.00 3.00 3 0.00 1.00 2.00 4 0.00 1.00 1.00 5 0.00 1.00 0.00(5 行受影响)
(r1-r2)-r2,可能不对,但是要实现这种效果,r3要计算出来
declare @t table (id int, r1 int , r2 int) insert @t select 1, 5, 0 union all select 2, 0, 2 union all select 3, 0, 1 union all select 4, 0, 1 union all select 5, 0, 1 Select *,(select sum([r1]-[r2]) from @T where [id]<=t.[id])r3 from @T t
Insert @T
select 1,5,0 union all
select 2,0,2 union all
select 3,0,1 union all
select 4,0,1 union all
select 5,0,1
Select
*,(select sum([r1]-[r2]) from @T where [id]<=t.[id])r3
from
@T t(5 個資料列受到影響)
id r1 r2 r3
----------- ----------- ----------- -----------
1 5 0 5
2 0 2 3
3 0 1 2
4 0 1 1
5 0 1 0(5 個資料列受到影響)
insert into @t1
select 1,5,0 union all
select 2,0,2 union all
select 3,0,1 union all
select 4,0,1 union all
select 5,0,1 SELECT id,sr,zc,(select sum(a.sr-a.zc) from @t1 a where a.id<=b.id) FROM @t1 b(5 行受影响)
id sr zc
----------- --------------------------------------- --------------------------------------- ---------------------------------------
1 5.00 0.00 5.00
2 0.00 2.00 3.00
3 0.00 1.00 2.00
4 0.00 1.00 1.00
5 0.00 1.00 0.00(5 行受影响)
insert @t select 1, 5, 0 union all select
2, 0, 2 union all select
3, 0, 1 union all select
4, 0, 1 union all select
5, 0, 1
Select
*,(select sum([r1]-[r2]) from @T where [id]<=t.[id])r3
from
@T t