我有两个MS 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)
答案 0 :(得分:0)
您希望结果在哪里?
进入变量或在控制台中显示?
尝试
SELECT (QUERY1) - (QUERY2)
用于控制台 和
DECLARE @myvar INTEGER
SELECT @myvar = (QUERY1) - (QUERY2)
表示变量。
答案 1 :(得分:0)
我认为问题是当你使用“赢了 - 未预订”时..我建议使用转义字符,看看它是怎么回事。校验 Escape a string in SQL Server so that it is safe to use in LIKE expression和Using Like