数据库sql语句写法
如图这样,写sql语句,后一年是前几年的和,以此类推! 用字查询实现,高手们,验证实力的时候来了! SQL 数据库
[解决办法]
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