2010-12-23 39 views
1
ALTER PROCEDURE [dbo].[MyStoredProcedure1] 
@YearToGet int 

AS 
Select Division, SDESCR,   
    DYYYY, 
Sum(APRICE) ASofSales, 
    Sum(PARTY) AS ASofPAX,   
    Sum(NetAmount) ASofNetSales,   
    Sum(InsAmount) ASofInsSales,   
    Sum(CancelRevenue) ASofCXSales,   
    Sum(OtherAmount) ASofOtherSales,   
    Sum(CXVALUE) ASofCXValue 
From dbo.B101BookingsDetails 
Where Booked <= CONVERT(int,DateAdd(year, @YearToGet - Year(getdate()),     
       DateAdd(day, DateDiff(day, 1, getdate()), 0))) and 
(DYYYY = @YearToGet) 
Group By SDESCR, DYYYY, Division 
Having (DYYYY = @YearToGet) 
Order By Division, SDESCR, DYYYY 

union all 

SELECT  
DIVISION, 
SDESCR, 
DYYYY, 
SUM(APRICE) AS YESales, 
SUM(PARTY) AS YEPAX, 
SUM(NetAmount) AS YENetSales, 
SUM(InsAmount) AS YEInsSales, 
SUM(CancelRevenue) AS YECXSales, 
SUM(OtherAmount) AS YEOtherSales, 
SUM(CXVALUE) AS YECXValue 
FROM   dbo.B101BookingsDetails 
Where ([email protected]) 
GROUP BY SDESCR, DYYYY, DIVISION 
ORDER BY DIVISION, SDESCR, DYYYY 

我得到的錯誤是UNION ALL不會在存儲過程中的工作

消息156,級別15,狀態1,過程 MyStoredProcedure1 36行錯誤 語法附近關鍵字工會」。

但在這裏我的目標是用戶輸入一個年份,例如2009年,我的第一個查詢將獲得在2009年做出它是昨天2009/12/23同一日期的所有銷售,而第二個查詢2009年獲得總額達12月31日2009年

我想列並排不是在一列

+0

而你得到的**錯誤信息是??你的問題是? – 2010-12-23 17:21:36

+1

當你試圖用`UNION ALL`將它們放在一起時,我不認爲你可以在你的部分`SELECT`上使用`ORDER BY`;嘗試從第一個`SELECT`中刪除ORDER BY` - 它工作嗎? – 2010-12-23 17:26:45

+1

你是正確的marc,但由於某種原因everythin顯示在相同的列例如YESales和AsofSales仍然在同一列 – MyHeadHurts 2010-12-23 17:29:47

回答

4

如果您想要並排顯示數據,那麼您不想使用UNION。相反,您可以使用派生表或CTE,然後加入SDESCR,DYYYY,Division中的2個查詢。查看你的查詢,唯一的(明顯的)區別是,在預訂列中包含where子句。我們可以將這個where子句條件移入總和聚合中,這應該允許我們通過表格獲得所需的所有數據。類似這樣的:

ALTER PROCEDURE [dbo].[MyStoredProcedure1] 
@YearToGet int 
AS 
SET NOCOUNT ON 

Declare @Booked Int 
Set @Booked = CONVERT(int,DateAdd(year, @YearToGet - Year(getdate()),     
       DateAdd(day, DateDiff(day, 1, getdate()), 0))) 

Select Division, 
     SDESCR,   
     DYYYY, 
     Sum(Case When Booked <= @Booked Then APRICE End) ASofSales, 
     SUM(APRICE) AS YESales, 

     Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,   
     SUM(PARTY) AS YEPAX, 

     Sum(Case When Booked <= @Booked Then NetAmount End) ASofNetSales,   
     SUM(NetAmount) AS YENetSales, 

     Sum(Case When Booked <= @Booked Then InsAmount End) ASofInsSales,   
     SUM(InsAmount) AS YEInsSales, 

     Sum(Case When Booked <= @Booked Then CancelRevenue End) ASofCXSales,   
     SUM(CancelRevenue) AS YECXSales, 

     Sum(Case When Booked <= @Booked Then OtherAmount End) ASofOtherSales,   
     SUM(OtherAmount) AS YEOtherSales, 

     Sum(Case When Booked <= @Booked Then CXVALUE End) ASofCXValue, 
     SUM(CXVALUE) AS YECXValue 
From dbo.B101BookingsDetails 
Where DYYYY = @YearToGet 
Group By SDESCR, DYYYY, Division 
Order By Division, SDESCR, DYYYY 

我鼓勵您嘗試此過程。我認爲它會返回您正在查找的數據。

被編輯爲小於或等於預定條件。

2

工會的要求包括該數據類型是相似的,每個SELECT包含相同的列數,並且這兩列在SELECT s中以相同順序出現。

編輯:在評論中回答你的問題:如果你想讓這些列並排顯示,你必須在兩個SELECT中都填入填充列。

Select Division, SDESCR, DYYYY, 
Sum(APRICE) ASofSales, 
'' AS YESales, 
Sum(PARTY) AS ASofPAX, 
'' AS YEPAX 

...等,具有相同的列都SELECTS,只是一些列是一個SELECT或其他空。

另一編輯:兩個SELECTS中的列名(或別名)不必相同,但使用不同的別名可以幫助您區分哪個列是哪個列。在聚合字段(SUM)上,如果您不提供別名,請確定該列代表的內容可能會非常棘手。如果你像在SQL中那樣對列進行別名並將空字符串列交錯,我想你會得到你所尋找的結果。

3

刪除第一個order by,就在union all之前。爲了理智,請保持列名相同。第二個order by適用於所有行。

BTW,你想要的更接近這個。沒有測試,但根據你的描述,這應該是接近。

; 
with 
q_00 as (
select 
     DIVISION 
    , SDESCR 
    , DYYYY 
    , sum(APRICE)  as ofSales 
    , sum(PARTY)   as ofPAX   
    , sum(NetAmount)  as ofNetSales   
    , sum(InsAmount)  as ofInsSales   
    , sum(CancelRevenue) as ofCXSales   
    , sum(OtherAmount) as ofOtherSales   
    , sum(CXVALUE)  as ofCXValue 
from dbo.B101BookingsDetails 
where Booked <= CONVERT(int,DateAdd(year, @YearToGet - Year(getdate()), DateAdd(day, DateDiff(day, 1, getdate()), 0))) 
    and DYYYY = @YearToGet 
group by DIVISION, SDESCR, DYYYY 
), 
q_01 as (
select  
     DIVISION 
    , SDESCR 
    , DYYYY 
    , sum(APRICE)  as YESales 
    , sum(PARTY)   as YEPAX 
    , sum(NetAmount)  as YENetSales 
    , sum(InsAmount)  as YEInsSales 
    , sum(CancelRevenue) as YECXSales 
    , sum(OtherAmount) as YEOtherSales 
    , sum(CXVALUE)  as YECXValue 
from dbo.B101BookingsDetails 
where [email protected] 
group by DIVISION, SDESCR, DYYYY 
) 
select 
     a.DIVISION 
    , a.SDESCR 
    , a.DYYYY 
    , ofSales 
    , ofPAX   
    , ofNetSales   
    , ofInsSales   
    , ofCXSales   
    , ofOtherSales   
    , ofCXValue 
    , YESales 
    , YEPAX 
    , YENetSales 
    , YEInsSales 
    , YECXSales 
    , YEOtherSales 
    , YECXValue 
from q_00 as a 
join q_01 as b on (b.DIVISION = a.DIVISION and b.SDESCR = a.SDESCR and b.DYYYY = a.DYYYY) 
order by a.DIVISION, a.SDESCR, a.DYYYY ; 
2

這裏的問題不是UNION ALL或列別名沒有命名相同。問題在於UNDER ALL行上方的ORDER BY行。如果您刪除該行,您的查詢將會運行。這不會像你想要的那樣列出數據,但它將允許查詢運行。