0
我試圖將兩個SELECT
語句合併爲一個使用SQL Server 2012 SP1的語句,以比較今年與去年的數字。加入兩個SELECT語句?
我遇到的問題是第一個SELECT重複12次(一年)。
我想是這樣的:
CY_Month_Year CY_Adviser_Commission PY_Month_Year PY_Adviser_Commission
April 2014 8462.561250000000 April 2013 14493.773250000000
August 2014 11092.801500000000 August 2013 20284.555500000000
這是輸出的電流例如:
CY_Month_Year CY_Adviser_Commission PY_Month_Year PY_Adviser_Commission
April 2014 8462.561250000000 April 2013 14493.773250000000
April 2014 8462.561250000000 August 2013 20284.555500000000
April 2014 8462.561250000000 December 2013 4782.624000000000
April 2014 8462.561250000000 February 2013 11581.927500000000
April 2014 8462.561250000000 January 2013 4823.082000000000
April 2014 8462.561250000000 July 2013 13607.822250000000
April 2014 8462.561250000000 June 2013 43582.885500000000
April 2014 8462.561250000000 March 2013 17677.662750000000
April 2014 8462.561250000000 May 2013 12642.168000000000
April 2014 8462.561250000000 November 2013 12698.284500000000
April 2014 8462.561250000000 October 2013 6730.836750000000
April 2014 8462.561250000000 September 2013 12339.673500000000
August 2014 11092.801500000000 April 2013 14493.773250000000
...
下面是SQL代碼,我到目前爲止有:
SELECT DISTINCT CY_Month_Year
,CY_Adviser_Commission
,PY_Month_Year
,PY_Adviser_Commission
FROM
-- Current year
(
SELECT DISTINCT DATE.Month_Name AS CY_Month_Year
,sum(ADV_COMM) AS CY_Adviser_Commission
FROM vw_Invoice_Data
INNER JOIN DATE ON vw_Invoice_Data.TransDate = DATE.PK_Date
WHERE DATE.Fiscal_Month BETWEEN '2014-01-01 00:00:00'
AND '2014-12-01 00:00:00'
AND vw_Invoice_Data.broker_code IN ('1-ABC')
GROUP BY DATE.month
,DATE.month_Name
) TY_Inv
,
-- Current year minus one
(
SELECT DISTINCT DATE.Month_Name AS PY_Month_Year
,sum(ADV_COMM) AS PY_Adviser_Commission
FROM vw_Invoice_Data id
INNER JOIN DATE ON id.TransDate = DATE.PK_Date
WHERE DATE.Fiscal_Month BETWEEN '2013-01-01 00:00:00'
AND '2013-12-01 00:00:00'
AND id.broker_code IN ('1-ABC')
GROUP BY DATE.month
,DATE.month_Name
) PY_Inv
任何幫助將不勝感激。
丹尼爾