[답변]피벗이 뭐죠?? 0 5 2,392

by 성시현 [2007.07.26 10:36:20]


편의상 중간결과 데이터들을 가지고만 만들었으니 실제 쿼리에 적용은 직접 해 보시길...;

 

테스트데이터...

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

 

건승하시길...수고하세요~~

 

by 송미진 [2007.07.26 00:00:00]
정말정말감사합니다.제눈엔 쿼리의 신으로 보이는군요...캬~~

by finecomp [2007.07.26 00:00:00]
컥...신이라니요...가당치 않습니다...;
원리만 조금 알면 누구나 가능한 쿼리입니다...;
차근차근 SQL 원리파악에 힘쓰시길...^^;

by 정순표 [2007.07.26 00:00:00]
성시현님 대단하신대요 ... 엔코아에서도 답글을 달고 계시네요 완전 존경!!!ㅎㅎ

by finecomp [2007.07.26 00:00:00]
헛...별말씀을...아직 많이 모자른데 부끄럽군요...;
아무튼 감사합니다...;

by 웁스 [2007.07.26 00:00:00]
finecomp 님이 성시현님이셨군요...
데이타베이스 사랑넷에서 많은 도움받았었는데
후후후!!
역시 고수님...
건승하세여..
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입