WITH t AS ( SELECT 'A' code, 1 gubun FROM dual UNION ALL SELECT 'A', 2 FROM dual UNION ALL SELECT 'B', 2 FROM dual UNION ALL SELECT 'A', 1 FROM dual UNION ALL SELECT 'B', 1 FROM dual UNION ALL SELECT 'A', 3 FROM dual UNION ALL SELECT 'C', 1 FROM dual UNION ALL SELECT 'B', 1 FROM dual UNION ALL SELECT 'A', 2 FROM dual UNION ALL SELECT 'C', 1 FROM dual UNION ALL SELECT 'D', 1 FROM dual ) SELECT DISTINCT gubun AS "구분" , SUM(cnt) AS "계" , SUM(code_a) AS code_a , SUM(code_b) AS code_b , SUM(code_c) AS code_c , SUM(code_d) AS code_d FROM ( SELECT gubun , COUNT(*) AS cnt , COUNT(DECODE(code, 'A', code)) AS code_a , COUNT(DECODE(code, 'B', code)) AS code_b , COUNT(DECODE(code, 'C', code)) AS code_c , COUNT(DECODE(code, 'D', code)) AS code_d FROM t GROUP BY gubun ) GROUP BY ROLLUP(gubun) ORDER BY gubun NULLS FIRST ; 퍼센트가 뭘 의미하는지 이해되지 않네요... 이 쿼리를 참조하셔서 수정하시거나, 다른 분의 쿼리를 참조하셔야 할 것 같네요...
WITH t AS ( SELECT 'A' cd, '1' gb FROM dual UNION ALL SELECT 'A', '2' FROM dual UNION ALL SELECT 'B', '2' FROM dual UNION ALL SELECT 'A', '1' FROM dual UNION ALL SELECT 'B', '1' FROM dual UNION ALL SELECT 'A', '3' FROM dual UNION ALL SELECT 'C', '1' FROM dual UNION ALL SELECT 'B', '1' FROM dual UNION ALL SELECT 'A', '2' FROM dual UNION ALL SELECT 'C', '1' FROM dual UNION ALL SELECT 'D', '1' FROM dual ) SELECT * FROM (SELECT NVL(gb, '합계') 구분1 , NVL(cd, '계') cd , COUNT(*) || '' 건수 , ROUND( RATIO_TO_REPORT(COUNT(*)) OVER( --PARTITION BY GROUPING_ID(gb, cd), NVL2(gb, cd, gb) -- 결과1 PARTITION BY GROUPING_ID(gb, cd), gb -- 결과2 ) * 100) || '%' 비율 FROM t GROUP BY CUBE(gb, cd) ) UNPIVOT (v FOR 구분2 IN (건수, 비율)) PIVOT (MIN(v) FOR cd IN ('계' 계, 'A' a, 'B' b, 'C' c, 'D' d)) ORDER BY DECODE(구분1, '합계', '0', 구분1), 구분2 ;