[답변] SYS_CONNECT_BY_PATH 이용 0 1 6,271

by 마농 계층쿼리 sys_connect_by_path [2008.09.12 13:32:31]


 

10G 에선 CONNECT_BY_ROOT 가 가능합니다.
9i 라면 SYS_CONNECT_BY_PATH 를 응용하시면 될듯.

SELECT NVL2(a.root_dept_cd,MIN(DECODE(a.lv,1,a.root_dept_nm)),'계') AS dept_nm
     , SUM(b.cnt) AS "계"
     , SUM(CASE WHEN b.work_grade_cd = '101' THEN b.cnt END) AS pre1
     , SUM(CASE WHEN b.work_grade_cd = '102' THEN b.cnt END) AS pre2
       -- 중략 --
     , SUM(CASE WHEN b.work_grade_cd = '130' THEN b.cnt END) AS pre23
     , SUM(CASE WHEN b.work_grade_cd = '131' THEN b.cnt END) AS pre24
  FROM (SELECT LEVEL lv
             , dept_cd
             , dept_nm
             , SUBSTR(SYS_CONNECT_BY_PATH(dept_cd,'-'),2,7) root_dept_cd
          FROM a_deptcd
         START WITH pdept_cd = '1000000' AND use_yn = 'Y'
        CONNECT BY PRIOR dept_cd = pdept_cd
        ) a
     , (SELECT b.dept_cd
             , b.work_grade_cd
             , COUNT(*) cnt
          FROM a_employee b
             , st_codevalue c
         WHERE b.retire_dt IS NULL
           AND b.work_grade_cd = c.code_value
           AND c.code_cd = 'C116'
           AND c.use_yn  = 'Y'
         GROUP BY b.dept_cd, b.work_grade_cd
        ) b
 WHERE a.deptcd = b.dept_cd(+)
 GROUP BY ROLLUP(a.root_dept_cd)
 ORDER BY GROUPING(a.root_dept_cd) DESC, a.root_dept_cd

by 손님 [2008.09.17 10:45:52]
감사합니다!^^
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입