sql面试题一道

已经知道原表
year salary
------------------ ---------------------
2000 1000
2001 2000
2002 3000
2003 4000
显示查询结果
year salary
------------------ ---------------------
2000 1000
2001 3000
2002 6000
2003 10000

即salary为以前年的工资的和

答案:

 1 use northwind
 2 go
 3 
 4 select * from orders
 5 
 6 select * from orders where orderDate between '1996-07-04' and '1996-07-20'
 7 
 8 select * from orders where  dateadd(day,500, orderDate)<getdate() 
 9 /*创建表和数据库*/
10 create database test
11 go
12 use test
13 go
14 create table Employee(
15     sId int identity (1,1primary key,
16     [Year] datetime ,
17     Salary decimal
18 )
19 go
20 insert Employee values('2001',1000)
21 insert Employee values('2002',2000)
22 insert Employee values('2003',3000)
23 insert Employee values('2004',5000)
24 /*查询语句*/
25 select * from Employee
26 /*答案*/
27 select a.[year],sum(b.[salary]from Employee as a,Employee as b where b.[year]<=a.[year] group by a.[year]
28 
29 
原文地址:https://www.cnblogs.com/seerlin/p/1330477.html