Declare @YearToGet int
Set @YearToGet = 2008
;With RawData As
(
Select 39503 As Booked
Union All Select 39509
Union All Select 39535
Union All Select 39620
Union All Select 39791
Union All Select 39838
Union All Select 39899
Union All Select 39134
Union All Select 39139
Union All Select 39139
Union All Select 39140
Union All Select 39146
Union All Select 39146
Union All Select 39146
)
, Numbers As
(
Select 0 As Value, Year(GetDate()) As [Year]
, Cast( DateDiff(d,0,GetDate()) as datetime ) As [Date]
, DateDiff(d,0,GetDate()) As [DateInt]
Union All
Select Value + 1, [Year] - 1
, DateAdd(yyyy, -1, [Date])
, DateDiff(d, 0, DateAdd(yyyy, -1, [Date]))
From Numbers
Where Value <= ( Year(GetDate()) - @YearToGet )
)
Select DIVISION, SDESCR, DYYYY
, SUM(APRICE) AS Sales
, SUM(PARTY) AS PAX
, SUM(NetAmount) AS NetSales
, SUM(InsAmount) AS InsSales
, SUM(CancelRevenue) AS CXSales
, SUM(OtherAmount) AS OtherSales
, SUM(CXVALUE) AS CXValue
From dbo.B101BookingsDetails As BD
Join Numbers As N
On Cast(N.[Year] As char(4)) = BD.DYYYY
Where BD.Booked = N.DateInt - 2
Group By DIVISION, SDESCR, DYYYY
答案 0 :(得分:3)
CREATE PROCEDURE dbo.MyStoredProcedure
@YearToGet int
AS
With RawData As
(
Select 39503 As Booked
Union All Select 39509
Union All Select 39535
Union All Select 39620
Union All Select 39791
Union All Select 39838
Union All Select 39899
Union All Select 39134
Union All Select 39139
Union All Select 39139
Union All Select 39140
Union All Select 39146
Union All Select 39146
Union All Select 39146
)
, Numbers As
(
Select 0 As Value, Year(GetDate()) As [Year]
, Cast( DateDiff(d,0,GetDate()) as datetime ) As [Date]
, DateDiff(d,0,GetDate()) As [DateInt]
Union All
Select Value + 1, [Year] - 1
, DateAdd(yyyy, -1, [Date])
, DateDiff(d, 0, DateAdd(yyyy, -1, [Date]))
From Numbers
Where Value <= ( Year(GetDate()) - @YearToGet )
)
Select DIVISION, SDESCR, DYYYY
, SUM(APRICE) AS Sales
, SUM(PARTY) AS PAX
, SUM(NetAmount) AS NetSales
, SUM(InsAmount) AS InsSales
, SUM(CancelRevenue) AS CXSales
, SUM(OtherAmount) AS OtherSales
, SUM(CXVALUE) AS CXValue
From dbo.B101BookingsDetails As BD
Join Numbers As N
On Cast(N.[Year] As char(4)) = BD.DYYYY
Where BD.Booked = N.DateInt - 2
Group By DIVISION, SDESCR, DYYYY
GO;
有关创建存储过程的详细信息,请查看此link。