日期:2014-05-17  浏览次数:20513 次

SQL 循环查询!~
SQL code

CREATE TABLE dbo.Employees
(
  Name   VarChar(40)   NOT NULL,
  Date   VarChar(8)   NOT NULL,
  Number Decimal(20,4) NOT NULL,
);
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120524', '50.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120408', '60.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120301', '40.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120201', '20.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120114', '10.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20111208', '50.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20111105', '30.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20111006', '40.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110908', '90.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110805', '50.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110705', '510.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110605', '120.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110505', '100.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110405', '210.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110305', '120.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110205', '30.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110105', '50.00');

Declare @Type Int
    Set @Type = 0

while(@Type < 9)
Begin
Select A.Name
      ,B.Date
      ,B.Number/C.Number-1
  From Employees A 
  Join Employees B  On A.Name = B.Name
  Join Employees C  On A.Name = C.Name
 Where A.Date = (Select Max(Date)
                   From Employees
                  Where Name = B.Name
                    And Date <= GetDate())
   And B.Date = (Select Max(Date)
                   From Employees
                  Where Name = B.Name
                    And Date <= Convert(VarChar(8),DateAdd(MM,-@Type,A.Date),112))
   And C.Date = (Select Max(Date)
                   From Employees
                  Where Name = B.Name
                    And Date <= Convert(VarChar(8),DateAdd(MM,-(@Type+1),A.Date),112))
 Set @Type = @Type + 1
End  

算法:
1.查询最近日期的数据
2.根据最新日期去过去10个月每个月的回报数据
Ps:不能使用 CREATE、Into 等关键字
谢谢!~
如何把我查询的结果整合在一张表中显示

--Drop Table Employees



------解决方案--------------------
那就用动态SQL了,如下
SQL code

CREATE TABLE dbo.Employees
(
  Name   VarChar(40)   NOT NULL,
  Date   VarChar(8)   NOT NULL,
  Number Decimal(20,4) NOT NULL,
);
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120524', '50.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120408', '60.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120301', '40.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120201', '20.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20120114', '10.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20111208', '50.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20111105', '30.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20111006', '40.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110908', '90.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110805', '50.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110705', '510.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110605', '120.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110505', '100.00');
Insert Into dbo.Employees(Name, Date, Number)  Values('张三','20110405', '210.00');
Insert Into dbo.Employees(Name,