trouble turning this into a stored procedure [closed]
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开发者_C百科(N.[Year] As char(4)) = BD.DYYYY
Where BD.Booked = N.DateInt - 2
Group By DIVISION, SDESCR, DYYYY
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;
For further information about creating Stored Procedures, check this link out.
精彩评论