GROUPING函數來排除group by cube數據問題

原由:group by cube的結果中出現兩條維度值同樣,彙總值不同的數據。sql

SELECT * FROM(      
            SELECT NVL(T.REG,'0000') AS REGION_ID,
                   NVL(T.PROJECT_TYPE, '0000') AS PROJECT_TYPE_ID,
                   NVL(T.VOLTAGE_LEVEL, '0000') AS VOL_CLASSIFY_ID,
                   NVL(T.CURRENT_NODE, '0000') AS NODE_ID,
                   NVL(T.TOTAL_CONTROL_INFO, '0000') AS TOTAL_CONTROL_TYPE_ID,
                   SUM(T.CN) AS CN,
                   SUM(T.AMT) AMT
              FROM TMP T
             WHERE 1 = 1
             GROUP BY CUBE(T.REG,
                           T.PROJECT_TYPE,
                           T.VOLTAGE_LEVEL,
                           T.CURRENT_NODE,
                           T.TOTAL_CONTROL_INFO)) T 
 WHERE 1=1
       AND T.REGION_ID='0000' 
       AND T.PROJECT_TYPE_ID='0000' 
       AND T.VOL_CLASSIFY_ID='0000' 
       AND T.TOTAL_CONTROL_TYPE_ID='75'
       AND T.NODE_ID='9';

使用GROUPING函數排除緣由:函數

SELECT * FROM(      
            SELECT NVL(T.REG,'0000') AS REGION_ID,
                   NVL(T.PROJECT_TYPE, '0000') AS PROJECT_TYPE_ID,
                   NVL(T.VOLTAGE_LEVEL, '0000') AS VOL_CLASSIFY_ID,
                   NVL(T.CURRENT_NODE, '0000') AS NODE_ID,
                   NVL(T.TOTAL_CONTROL_INFO, '0000') AS TOTAL_CONTROL_TYPE_ID,
                   grouping(REG) AS REG,
                   grouping(PROJECT_TYPE) AS PROJECT_TYPE,
                   grouping(VOLTAGE_LEVEL) AS VOLTAGE_LEVEL, 
                   grouping(TOTAL_CONTROL_INFO) AS TOTAL_CONTROL_INFO,
                   grouping(TOTAL_CONTROL_INFO) AS TOTAL_CONTROL_INFO,
                   SUM(T.CN) AS CN,
                   SUM(T.AMT) AMT
              FROM TMP T
             WHERE 1 = 1
             GROUP BY CUBE(T.REG,
                           T.PROJECT_TYPE,
                           T.VOLTAGE_LEVEL,
                           T.CURRENT_NODE,
                           T.TOTAL_CONTROL_INFO)) T 
 WHERE 1=1
       AND T.REGION_ID='0000' 
       AND T.PROJECT_TYPE_ID='0000' 
       AND T.VOL_CLASSIFY_ID='0000' 
       AND T.TOTAL_CONTROL_TYPE_ID='75'
       AND T.NODE_ID='9';

經過GROUPING函數能夠看出,一條數據使用了reg來進行彙總,一條則沒有使用,這個時候就能夠看出來,源表中存在reg爲0000的髒數據。code

解釋:blog

  如上結果中GROUPING函數返回值「0」和「1」的含義。
  若是顯示「1」表示CUBE函數對應的列(例如REG字段)是因爲CUBE函數所產生的空值對應的信息,即對此列進行彙總計算後的結果。
  若是顯示「0」表示此行對應的這列參未與分組彙總活動。class

相關文章
相關標籤/搜索