日期:2014-05-17 浏览次数:20826 次
create table #a
(
a1 int,
a2 int
)
go
insert into #a(a1,a2)values(100,0)
insert into #a(a1,a2)values(-20,0)
insert into #a(a1,a2)values(60,0)
insert into #a(a1,a2)values(300,0)
go
select * from #a
;with tb as (
select row_number() over( order by a2) as id,a1,a2 from #a
)
select tb.a1,sum(tb1.a1)as a1sum from tb inner join tb as tb1 on tb1.id<=tb.id
group by tb.a1,tb.id
go
--或者
--drop table #b
select id =IDENTITY(int,1,1) ,a1,a2 INTO #b from #a
go
select x.a1,sum(isnull(y.a1,0)) as a2 from #b x inner join #b y on y.id <= x.id
group by x.a1,x.id