편의상 중간결과 데이터들을 가지고만 만들었으니 실제 쿼리에 적용은 직접 해 보시길...;
테스트데이터...
WITH
t AS
(
SELECT '전략실' AS highdeptname, 'C10S0969' AS deptno, TRIM('개발팀 ') AS deptname, 1 AS dept_cnt, trim('2단계완료') AS status FROM dual UNION ALL
SELECT '전략실' AS highdeptname, 'C10S0969' AS deptno, TRIM('개발팀 ') AS deptname, 1 AS dept_cnt, trim('3단계완료') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0961' AS deptno, TRIM('6/T그룹') AS deptname, 1 AS dept_cnt, trim('3단계완료') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0971' AS deptno, TRIM('VIP ') AS deptname, 2 AS dept_cnt, trim('1단계완료') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0971' AS deptno, TRIM('VIP ') AS deptname, 1 AS dept_cnt, trim('2단계완료') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0971' AS deptno, TRIM('VIP ') AS deptname, 2 AS dept_cnt, trim('3단계완료') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0972' AS deptno, TRIM('기구 ') AS deptname, 3 AS dept_cnt, trim('3단계완료') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0973' AS deptno, TRIM('프로 ') AS deptname, 2 AS dept_cnt, trim('3단계완료') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0973' AS deptno, TRIM('프로 ') AS deptname, 1 AS dept_cnt, trim('선정 ') AS status FROM dual UNION ALL
SELECT '혁신팀' AS highdeptname, 'C10S0979' AS deptno, TRIM('회로 ') AS deptname, 3 AS dept_cnt, trim('3단계완료') AS status FROM dual
)
9i 이상 쿼리...
SELECT
DECODE(GROUPING(highdeptname), 1, '-', highdeptname) AS highdeptname
, DECODE(GROUPING(deptno), 1, '-', deptno) AS deptno
, DECODE(GROUPING(deptname), 1, '-', deptname) AS deptname
, NVL(TO_CHAR(SUM(DECODE(status, '1단계완료', dept_cnt))), '-') AS "1단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '2단계완료', dept_cnt))), '-') AS "2단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '3단계완료', dept_cnt))), '-') AS "3단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '4단계완료', dept_cnt))), '-') AS "4단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '5단계완료', dept_cnt))), '-') AS "5단계완료"
FROM t
GROUP BY ROLLUP((highdeptname, deptno, deptname))
ORDER BY highdeptname, deptno, deptname
9i 이전 쿼리...
SELECT
DECODE(GROUPING(highdeptname), 1, '-', highdeptname) AS highdeptname
, DECODE(GROUPING(deptno), 1, '-', deptno) AS deptno
, DECODE(GROUPING(deptname), 1, '-', deptname) AS deptname
, NVL(TO_CHAR(SUM(DECODE(status, '1단계완료', dept_cnt))), '-') AS "1단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '2단계완료', dept_cnt))), '-') AS "2단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '3단계완료', dept_cnt))), '-') AS "3단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '4단계완료', dept_cnt))), '-') AS "4단계완료"
, NVL(TO_CHAR(SUM(DECODE(status, '5단계완료', dept_cnt))), '-') AS "5단계완료"
FROM t
GROUP BY ROLLUP(highdeptname, deptno, deptname)
HAVING GROUPING(highdeptname) + GROUPING(deptno) + GROUPING(deptname) = 0
OR GROUPING(highdeptname) + GROUPING(deptno) + GROUPING(deptname) = 3
ORDER BY highdeptname, deptno, deptname
건승하시길...수고하세요~~