连接两个 SELECT 语句?
Joining two SELECT statements?
我正在尝试使用 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
如有任何帮助,我们将不胜感激。
丹尼尔
更新了其他选项!
我已经使用了您的查询,形成了一个带有月份的 id 列,并将它们加入其中。
SELECT DISTINCT CY_Month_Year,
CY_Adviser_Commission,
PY_Month_Year,
PY_Adviser_Commission
FROM
-- Current year
(SELECT DISTINCT Substring(Month_Name, 0, Len(Month_Name) - 4) AS id,
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
JOIN
-- Current year minus one
(SELECT DISTINCT Substring(Month_Name, 0, Len(Month_Name) - 4) AS id,
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
ON TY_Inv.id = PY_Inv.id
所以这通常会连接两个不同年份的月份!
这个也可以试试
;WITH Y1
AS (SELECT MONTH(TRANSDATE)AS MONTHS,
MONTH_NAME,
ADV_COMM
FROM VW_INVOICE_DATA
INNER JOIN DATE
ON VW_INVOICE_DATA.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' )),
Y2
AS (SELECT MONTH(TRANSDATE)AS MONTHS,
MONTH_NAME,
ADV_COMM
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 ID.BROKER_CODE IN ( '1-ABC' ))
SELECT Y1.MONTH_NAME,
SUM(Y1.ADV_COMM),
Y2.MONTH_NAME,
SUM(Y2.ADV_COMM)
FROM Y1
JOIN Y2
ON Y1.MONTHS = Y2.MONTHS
GROUP BY Y1.MONTH_NAME,
Y2.MONTH_NAME
如果您在发票数据中有日期列 table 您可以避免加入 "date" 并直接使用 year(datecolumn)=2013/2014/...
我正在尝试使用 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
如有任何帮助,我们将不胜感激。
丹尼尔
更新了其他选项! 我已经使用了您的查询,形成了一个带有月份的 id 列,并将它们加入其中。
SELECT DISTINCT CY_Month_Year,
CY_Adviser_Commission,
PY_Month_Year,
PY_Adviser_Commission
FROM
-- Current year
(SELECT DISTINCT Substring(Month_Name, 0, Len(Month_Name) - 4) AS id,
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
JOIN
-- Current year minus one
(SELECT DISTINCT Substring(Month_Name, 0, Len(Month_Name) - 4) AS id,
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
ON TY_Inv.id = PY_Inv.id
所以这通常会连接两个不同年份的月份!
这个也可以试试
;WITH Y1
AS (SELECT MONTH(TRANSDATE)AS MONTHS,
MONTH_NAME,
ADV_COMM
FROM VW_INVOICE_DATA
INNER JOIN DATE
ON VW_INVOICE_DATA.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' )),
Y2
AS (SELECT MONTH(TRANSDATE)AS MONTHS,
MONTH_NAME,
ADV_COMM
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 ID.BROKER_CODE IN ( '1-ABC' ))
SELECT Y1.MONTH_NAME,
SUM(Y1.ADV_COMM),
Y2.MONTH_NAME,
SUM(Y2.ADV_COMM)
FROM Y1
JOIN Y2
ON Y1.MONTHS = Y2.MONTHS
GROUP BY Y1.MONTH_NAME,
Y2.MONTH_NAME
如果您在发票数据中有日期列 table 您可以避免加入 "date" 并直接使用 year(datecolumn)=2013/2014/...