分组汇总时如何排序

时间:2018-11-29 05:28:25

标签: sql database oracle

我要在Z.PRIORITY之前下订单。但是请阻止ORA-00979

如何通过Z.PRIORITY添加订单?我不想按Z.PRIORITY分组。

尝试分组和排序,但TOTALSUM出现错误

    SELECT 
    A.CCM_NO,
    CASE
        WHEN GROUPING(A.PLANT_CD) = 0 AND GROUPING(A.CCM_NO) = 1 THEN 'sum'
        WHEN GROUPING(A.PLANT_CD) = 1 THEN 'totalsum'
        ELSE MAX(Z.CODE_NM)
    END PLANT_NM,
    CASE
        WHEN GROUPING(A.PLANT_CD) = 0 AND GROUPING(A.CCM_NO) = 1 THEN COUNT(1)
        WHEN GROUPING(A.PLANT_CD) = 1 THEN COUNT(1)
        ELSE COUNT(1)
    END CNT_II,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.PLANT_CD )) PLANT_CD,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(Y.HIER_NM )) ITEM_HIER,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.COMP_CD )) COMP_CD,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(AB.HIER_NM )) HIER_NM3,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(DECODE(FX_CODE_NM('COMM', 'BC0010', A.COMP_CD), 'Unknown Code', '', FX_CODE_NM('COMM', 'BC0010', A.COMP_CD)))) COMP_NM,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.ITEM_CD )) ITEM_CD,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(K.ITEM_NM )) ITEM_NM,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(M.HIER_NM )) CM_HIER,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(N.HIER_NM)) CM_HIER2,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.CCM_BODY )) CCM_BODY,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.CCM_HEAD )) CCM_HEAD,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.LIMIT_DT )) LIMIT_DT,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.TREAT_TXT )) TREAT_TXT,
        DECODE(GROUPING(A.CCM_NO), 1, '', MAX(DECODE(FX_CODE_NM('COMM', 'CM0070', A.TREAT_CD), 'Unknown Code', '', FX_CODE_NM('COMM', 'CM0070', A.TREAT_CD)))) TREAT_NM,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(DECODE(FX_CODE_NM('COMM', 'CM0020', A.STATUS), 'Unknown Code', '', FX_CODE_NM('COMM', 'CM0020', A.STATUS)))) STATUS_NM,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(A.STATUS) ) STATUS,
    SUM(
        SELECT DISTINCT (SUM(X.RWD_AMT) OVER (PARTITION BY X.CCM_NO)) TOT_AMT
        FROM CM_CCM_FB_RWD X
        WHERE X.CCM_NO = A.CCM_NO
    ) TOT_AMT_II,
    DECODE(GROUPING(A.CCM_NO), 1, '', MAX(P.PNLTY_NM)) PNLTY_NM,
    SUM(P.PNLTY_AMT) PNLTY_AMT_II,
    'total' AS LOCAL_NM1,
    'total' AS LOCAL_NM2
FROM 
    CM_CCM A
    LEFT OUTER JOIN LM_COMMON_LINE Z 
        ON  Z.CODE = A.PLANT_CD
        AND Z.REF_CHR1 = A.COMP_CD
        AND Z.HEAD_CD IN ('CM0150')
        AND Z.USE_YN = 'Y'
    LEFT OUTER JOIN BC_ITEM K 
        ON  K.COMP_CD = A.COMP_CD
        AND K.ITEM_CD = A.ITEM_CD
    LEFT OUTER JOIN BC_HIERARCHY Y
        ON Y.COMP_CD = K.COMP_CD
        AND Y.HIER_CD = SUBSTR(K.HIRCHY_CD, 0, 8)
        AND Y.HIER_TP = 'ITEM'
        AND Y.HIER_LVL = '2'
    LEFT OUTER JOIN CM_HIERARCHY M
        ON  M.HIER_CD = A.HIER_CD
        AND M.HIER_TP = 'CCM'
    LEFT OUTER JOIN CM_HIERARCHY N
        ON  N.HIER_CD = SUBSTR(A.HIER_CD , 1 , 6)
        AND N.HIER_TP = 'CCM' 
    LEFT OUTER JOIN CM_PENALTY_GR P
        ON  P.PNLTY_GR = A.PNLTY_GR
    LEFT OUTER JOIN CM_CUST CT
        ON  A.CUST_NO = CT.CUST_NO 
    LEFT OUTER JOIN LM_COMMON_LINE LC 
        ON  SUBSTR(CT.ZIP_CD, 0, 3) = LC.CODE
        AND LC.HEAD_CD = 'CM0212'
WHERE 
    1=1
GROUP BY
    ROLLUP( A.PLANT_CD , A.CCM_NO)
ORDER BY 
    Z.PRIORITY,
    A.PLANT_CD,
    A.CCM_NO ASC 

0 个答案:

没有答案