开发者

trouble turning this into a stored procedure [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜