2010-12-09 21 views
0
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 
+1

Quelle erreur obtenez-vous? – JNK

+1

ce n'est pas une question! veuillez préciser ce que vous avez essayé et quoi/où cela s'est mal passé – KBoek

Répondre

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; 

Pour plus d'informations sur la création de procédures stockées, vérifier ceci link.

+0

LOL avez-vous simplement ajouté un GO? Si oui, vous obtenez un +1 de moi ... – JNK

+1

Ne pas oublier la procédure de création; o) –

+0

l'ai eu .......... – MyHeadHurts