22,210
社区成员
发帖
与我相关
我的任务
分享
bm sl
A 1
B 2
A 3
A -1
B 3
C 1
A 5
bm sl leiji
A 1 1
B 2 2
A 3 4
A -1 3
B 3 5
C 1 1
A 5 8
select bm,sl,sum(sl)over(partition by bm order by rid)leiji
from
(select *,rid=row_number()over(order by (select 1)) from #t)t
order by rid
create table #t(bm varchar(10),sl int)
insert into #t
select 'A',1 union all
select 'B',2 union all
select 'A',3 union all
select 'A',-1 union all
select 'B',3 union all
select 'C',1 union all
select 'A',5
with t as
(select rn=row_number() over(order by getdate()),* from #t
)
select bm,sl,leiji=(select sum(b.sl) from t b where b.bm=a.bm and b.rn<=a.rn)
from t a
/*
bm sl leiji
---------- ----------- -----------
A 1 1
B 2 2
A 3 4
A -1 3
B 3 5
C 1 1
A 5 8
(7 行受影响)
*/