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
Répondre
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;
Pour plus d'informations sur la création de procédures stockées, vérifier ceci link.
LOL avez-vous simplement ajouté un GO? Si oui, vous obtenez un +1 de moi ... – JNK
Ne pas oublier la procédure de création; o) –
l'ai eu .......... – MyHeadHurts
Quelle erreur obtenez-vous? – JNK
ce n'est pas une question! veuillez préciser ce que vous avez essayé et quoi/où cela s'est mal passé – KBoek