WITH T(CLASS, NAME, WEIGHT) AS ( SELECT '1반', '철수', 50 FROM DUAL UNION ALL SELECT '1반', '영희', 60 FROM DUAL UNION ALL SELECT '2반', '호동', 100 FROM DUAL ) SELECT DECODE(GROUPING(NAME), 0, CLASS, GROUPING(CLASS), '총계', '소계') CLASS, NAME, SUM(WEIGHT), DECODE(GROUPING(NAME), 1, 0, ROUND(SUM(WEIGHT) / MAX(C_S) * 100, 2) ) C_R, DECODE(GROUPING(NAME), 1, 0, ROUND(SUM(WEIGHT) / MAX(T_S) * 100, 2) ) T_R FROM ( SELECT CLASS, NAME, WEIGHT, SUM(WEIGHT) OVER(PARTITION BY CLASS) C_S, SUM(WEIGHT) OVER() T_S FROM T ) GROUP BY ROLLUP(CLASS, NAME)
WITH t AS ( SELECT '1반' cls, '철수' nm, 50 wgt FROM dual UNION ALL SELECT '1반', '영희', 60 FROM dual UNION ALL SELECT '2반', '호동', 100 FROM dual ) SELECT NVL(cls, '합계') cls , DECODE(gid, 0, nm, 1, '소계') nm , wgt , DECODE(gid, 0, ROUND(RATIO_TO_REPORT(wgt) OVER(PARTITION BY gid, cls)*100, 2)) c_r , DECODE(gid, 0, ROUND(RATIO_TO_REPORT(wgt) OVER(PARTITION BY gid )*100, 2)) t_r FROM (SELECT cls, nm , SUM(wgt) wgt , GROUPING_ID(cls, nm) gid FROM t GROUP BY ROLLUP(cls, nm) ) a ORDER BY a.cls, a.nm ;