2014-05-09 45 views
0

我有兩個MS SQL Server查詢,當單獨運行時會產生正確的結果,但是當我嘗試從另一個結果中減去結果時,我收到錯誤:SQL Server不正確的語法在' - '附近減法

Incorrect syntax near '-'. 

有什麼想法?我一遍又一遍地計算了括號,但我可能完全錯過了一些東西。

完整的查詢是:

GO 
declare @oldDate DATETIME = '3/31/2014'; 
declare @newDate DATETIME = '5/1/2014'; 

(SELECT COALESCE(SUM([Revenue]), 0) FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
WHERE [Opp_ID] IN (
    -- Select everything in the new date 
    SELECT [Opp_ID] FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
    WHERE [Stage] = '6. Upside' 
    AND ([Status] = 'Won - Not Booked' OR [Status] = 'Open') AND [Report_Date] = @newDate 

    Except 
    -- everything in the old date 
    (SELECT [Opp_ID] FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
    WHERE [Stage] = '6. Upside' 
    AND ([Status] = 'Won - Not Booked' OR [Status] = 'Open') AND [Report_Date] = @oldDate 
    Union 
    -- everything that was a new item 
    SELECT [Opp_ID] FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
     WHERE [Opp_ID] NOT IN (SELECT [Opp_ID] FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
     WHERE [Report_Date] = @oldDate) 
    AND [Stage] = '6. Upside' 
    AND ([Status] = 'Won - Not Booked' OR [Status] = 'Open') AND [Report_Date] = @newDate 
    Union 
    -- everything that was added to upside (stage change) 
    SELECT [t].[Opp_ID] 
     FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel][t] 
     Join [authmanager2].[dbo].[cboyd_monthly_finance_funnel][s] On [t].[Opp_ID] = [s].[Opp_ID] 
     WHERE [t].[Report_Date] = @newDate And [t].[Stage] = '6. Upside' 
     And ([t].[Status] = 'Open' Or [t].[Status] = 'Won - Not Booked') 
     And ([s].[Stage] = '7. Forecast' And [s].[Report_Date] = @oldDate) 
    ) 
) AND [Report_Date] = @newDate) 
- 
(SELECT COALESCE(SUM([Revenue]), 0) FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
WHERE [Opp_ID] IN (
    -- Select everything in the old date 
    SELECT [Opp_ID] FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
    WHERE [Stage] = '6. Upside' 
    AND ([Status] = 'Won - Not Booked' OR [Status] = 'Open') AND [Report_Date] = @oldDate 

    Except 
    -- everything that was taken from upside (stage change) 
    (-- Select everything in the new date 
    SELECT [Opp_ID] FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel] 
     WHERE [Stage] = '6. Upside' 
     AND ([Status] = 'Won - Not Booked' OR [Status] = 'Open') AND [Report_Date] = @newDate 
    Union 
    SELECT [t].[Opp_ID] 
     FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel][t] 
     Join [authmanager2].[dbo].[cboyd_monthly_finance_funnel][s] On [t].[Opp_ID] = [s].[Opp_ID] 
     WHERE [t].[Report_Date] = @newDate And [t].[Stage] = '7. Forecast' 
     And ([t].[Status] = 'Open' Or [t].[Status] = 'Won - Not Booked') 
     And ([s].[Stage] = '6. Upside' And [s].[Report_Date] = @oldDate) 
    Union 
    -- everything that was booked 
    SELECT [s].[Opp_ID] 
     FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel][t] 
     Join [authmanager2].[dbo].[cboyd_monthly_finance_funnel][s] On [t].[Opp_ID] = [s].[Opp_ID] 
     WHERE [s].[Stage] = '6. Upside' AND [t].[Report_Date] = @newDate And [t].[Status] = 'Closed - Won' 
     And [s].[Report_Date] = @oldDate And [s].[Status] != 'Closed - Won' 
    Union 
    -- everything that went to proposed 
    SELECT [t].[Opp_ID] 
     FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel][t] 
     Join [authmanager2].[dbo].[cboyd_monthly_finance_funnel][s] On [t].[Opp_ID] = [s].[Opp_ID] 
     WHERE [t].[Report_Date] = @newDate And [t].[Stage] = '5. Proposed' 
     And ([t].[Status] = 'Open' Or [t].[Status] = 'Won - Not Booked') 
     And ([s].[Stage] = '6. Upside' And [s].[Report_Date] = @oldDate) 
     And ([s].[Status] = 'Open' Or [s].[Status] = 'Won - Not Booked') 
    Union 
    -- everything that was lost 
    SELECT [s].[Opp_ID] 
     FROM [authmanager2].[dbo].[cboyd_monthly_finance_funnel][t] 
     Join [authmanager2].[dbo].[cboyd_monthly_finance_funnel][s] On [t].[Opp_ID] = [s].[Opp_ID] 
     WHERE [s].[Stage] = '6. Upside' AND [t].[Report_Date] = @newDate And ([t].[Status] = 'Closed - Lost' Or [t].[Status] = 'Inactive') 
     And [s].[Report_Date] = @oldDate And ([s].[Status] != 'Closed - Lost' And [s].[Status] != 'Inactive') 
    ) 
) AND [Report_Date] = @oldDate) 
+1

您不能在兩個查詢之間使用'-'。可以在前面加一個「select」來減去兩個標量,或者使用「except」來設置「減法」。 –

+1

查詢是否自動生成?所有那些方括號都是沒用的 – Serpiton

+0

@戈登林諾夫是對的。我當時是一個白癡,因爲我把它從我的主要程序中拖出來並重寫了它。我包裝整個事情就像這樣,它的工作原理(沒有足夠的代表回答我自己的問題):'SELECT DISTINCT(query1) - (query2) FROM [authmanager2]。[dbo]。[cboyd_monthly_finance_funnel];' – jackbanditdude

回答

0

你想去哪裏,結果去了?

變成一個變量或顯示在控制檯?

嘗試

SELECT (QUERY1) - (QUERY2) 

的控制檯 和

DECLARE @myvar INTEGER 
SELECT @myvar = (QUERY1) - (QUERY2) 

一個變量。

相關問題