1. 程式人生 > >ORACLE中每個分組後加個合計,最後總合計的GROUP BY rollup,和中文排序

ORACLE中每個分組後加個合計,最後總合計的GROUP BY rollup,和中文排序

參考程式碼如下

SELECT LOCAL_NAME ,
    DEPT_DISTINGUISH_NAME ,
    SUM(TOTAL_PAY_SUM_R_LDC) TOTAL_PAY_SUM_R_LDC ,
    SUM(BASE_PAY_R_LDC) BASE_PAY_R_LDC 
  FROM PA_SUMMARY_B04
  WHERE PA_MONTH='201509'
  GROUP BY rollup(DEPT_DISTINGUISH_NAME,LOCAL_NAME)


得到的會是同一個部門區分下的人員分組,每個部門區分最後加一個合計。最後加一個總計。

在這裡可以用到排序時,用一個列

 ROW_NUMBER() over( order by NLSSORT(PA.DEPT_DISTINGUISH_NAME,'NLS_SORT = SCHINESE_PINYIN_M'), to_number(GET_GLOBAL_NAME(PA.POST_GRADE_NO,'zh')) DESC) NUM

然後排序時中文按字母排序用如下

ORDER BY NLSSORT(A.DEPT_DISTINGUISH_NAME,'NLS_SORT = SCHINESE_PINYIN_M'),
  B.NUM,
  B.local_name ;