在某论坛上看有网友发帖说面试时被考到了这样一道题:
数据:
Year Salary
2000 1000
2001 2000
2002 3000
2003 4000
查询结果:
Year Salary
2000 1000
2001 3000
2002 6000
2003 10000
自己在SQL Server里试了一下:
if exists (select * from sysobjects where id = object_id(N'Test'))
drop table Test
create table Test (year int, salary float)
insert into Test values (2000, 1000)
insert into Test values (2001, 2000)
insert into Test values (2002, 3000)
insert into Test values (2003, 4000)
select a.year, sum(b.salary) as salary
from test as a left outer join test as b on a.year >= b.year
group by a.year order by a.year
顺利通过。很久没怎么写SQL语句了。

没有评论:
发表评论