加入两个SELECT语句?

时间:2015-01-13 06:22:03

标签: sql-server tsql select join union

我正在尝试将两个SELECT语句合并为一个使用SQL Server 2012 SP1,以比较今年与去年的数据。

我遇到的问题是第一个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

非常感谢任何帮助。

丹尼尔

1 个答案:

答案 0 :(得分:1)

更新了其他选项! 我已经使用了你的查询,用月形成了一个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

如果您在发票数据表中有日期列,则可以避免加入" date"并直接使用年份(datecolumn)= 2013/2014 /...