2015-01-13 161 views
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 

任何幫助將不勝感激。

丹尼爾

回答

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 

如果u有發票數據表的日期列可以避開加盟「日期」,並直接使用一年(datecolumn)=二千零十四分之二千零一十三/ ...