2017-02-17 53 views
0

我有一個完整的外連接查詢,在oracle中有子查詢和子查詢。我試圖完成的是將當年的數據和前一年的數據彙總在同一張表中,以便對其進行比較。但是,我的FULL OUTER JOIN充當內連接,不返回當前年份和上一年的空值。完全外連接「case when」和子查詢

這裏是我的代碼:

SELECT 
    SQ1.CHANNEL, 
    SQ1.SHORT, 
    SQ1.NAME, 
    SQ1.RDC, 
    SQ1.CY_APPROVED_COUNT, 
    SQ2.PY_APPROVED_COUNT, 
    SQ1.CY_APPROVED_VOLUME, 
    SQ2.PY_APPROVED_VOLUME, 
    SQ1.CY_DECLINED_COUNT, 
    SQ2.PY_DECLINED_COUNT, 
    SQ1.CY_DECLINED_VOLUME, 
    SQ2.PY_DECLINED_VOLUME, 
    SQ1.CY_RETURNED_COUNT, 
    SQ2.PY_RETURNED_COUNT, 
    SQ1.CY_RETURNED_VOLUME, 
    SQ2.PY_RETURNED_VOLUME 
FROM (SELECT 
      CHANNEL, 
      SHORT, 
      NAME, 
      RDC, 
      SUM (CASE WHEN STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS CY_APPROVED_COUNT, 
      SUM (CASE WHEN STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS CY_APPROVED_VOLUME, 
      SUM (CASE WHEN STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS CY_DECLINED_COUNT, 
      SUM (CASE WHEN STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS CY_DECLINED_VOLUME, 
      SUM (CASE WHEN STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS CY_RETURNED_COUNT, 
      SUM (CASE WHEN STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS CY_RETURNED_VOLUME 
     FROM WFRT_MSP_SP_MTD 
     WHERE PERIOD >= TO_DATE('2016/02/01', 'yyyy/mm/dd') 
     AND PERIOD <= TO_DATE('2016/02/13','yyyy/mm/dd') 
     AND CHANNEL = 'MSP' 
     AND RDC = 'BASE' 
     GROUP BY 
      CHANNEL, 
      SHORT, 
      NAME, 
      RDC 
) SQ1 
-- NOT CORRECTLY SHOWING NULL VALUES 
FULL OUTER JOIN 

(SELECT 
    CHANNEL, 
    SHORT, 
    NAME, 
    RDC, 
    SUM (CASE WHEN STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS PY_APPROVED_COUNT, 
    SUM (CASE WHEN STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS PY_APPROVED_VOLUME, 
    SUM (CASE WHEN STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS PY_DECLINED_COUNT, 
    SUM (CASE WHEN STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS PY_DECLINED_VOLUME, 
    SUM (CASE WHEN STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS PY_RETURNED_COUNT, 
    SUM (CASE WHEN STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS PY_RETURNED_VOLUME 
FROM WFRT_MSP_SP_MTD 
WHERE PERIOD >= TO_DATE('2015/02/01', 'yyyy/mm/dd') 
    AND PERIOD <= TO_DATE('2015/02/13','yyyy/mm/dd') 
    AND CHANNEL = 'MSP' 
    AND RDC = 'BASE' 
GROUP BY 
    CHANNEL, 
    SHORT, 
    NAME, 
    RDC 
) SQ2 
ON sq1.short = sq2.short 
; 

如果可以請幫幫忙。

+1

可能有助於使用一些縮進可讀性 –

回答

2

只需使用條件彙總:

SELECT CHANNEL, SHORT, NAME, RDC, 
     SUM(CASE WHEN this_year = 1 AND STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS CY_APPROVED_COUNT, 
     SUM(CASE WHEN this_year = 1 AND STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS cY_APPROVED_VOLUME, 
     SUM(CASE WHEN this_year = 1 AND STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS CY_DECLINED_COUNT, 
     SUM(CASE WHEN this_year = 1 AND STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS CY_DECLINED_VOLUME, 
     SUM(CASE WHEN this_year = 1 AND STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS CY_RETURNED_COUNT, 
     SUM(CASE WHEN this_year = 1 AND STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS CY_RETURNED_VOLUME, 
     SUM(CASE WHEN prev_year = 1 AND STATUS = 'Approved' THEN APP_COUNTS ELSE 0 END) AS PY_APPROVED_COUNT, 
     SUM(CASE WHEN prev_year = 1 AND STATUS = 'Approved' THEN PROJ_VOL ELSE 0 END) AS PY_APPROVED_VOLUME, 
     SUM(CASE WHEN prev_year = 1 AND STATUS = 'Declined' THEN APP_COUNTS ELSE 0 END) AS PY_DECLINED_COUNT, 
     SUM(CASE WHEN prev_year = 1 AND STATUS = 'Declined' THEN PROJ_VOL ELSE 0 END) AS PY_DECLINED_VOLUME, 
     SUM(CASE WHEN prev_year = 1 AND STATUS = 'Returned' THEN APP_COUNTS ELSE 0 END) AS PY_RETURNED_COUNT, 
     SUM(CASE WHEN prev_year = 1 AND STATUS = 'Returned' THEN PROJ_VOL ELSE 0 END) AS PY_RETURNED_VOLUME 
FROM (SELECT msm.*, 
      (CASE WHEN PERIOD >= DATE '2015-02-01' AND 
         PERIOD <= '2015-02-13' 
        THEN 1 ELSE 0 
       END) as prev_year, 
      (CASE WHEN PERIOD >= DATE '2016-02-01' AND 
         PERIOD <= '2016-02-13' 
        THEN 1 ELSE 0 
       END) as this_year 
     FROM WFRT_MSP_SP_MTD msm 
    ) msm 
WHERE CHANNEL = 'MSP' AND RDC = 'BASE' 
GROUP BY CHANNEL, SHORT, NAME, RDC; 
+0

我得到ORA-00904: 「MSM」:無效的標識符 00904. 00000 - 「%s的:無效的標識符」 – dmoses

+0

@dmoses只是簡單地將'msm'別名從子查詢的閉括號外移到它的內部(即,您需要在子查詢內部而不是子查詢本身中別名)。 – Boneist

+0

@Boneist我在子查詢「wfrt_msp_sp_mtd」旁邊的表格旁邊移動了「msm」,並收到錯誤消息「文字與格式字符串不匹配」 – dmoses