계층 구조로 되어 있는 부서정보를 펼쳐줘야 할일이 있습니다.
1 ㄱㄱ부서
1.1 ㄴㄴ부서
1.1.1 ㄷㄷ부서
1.1.1.1 ㅈㅈ부서
2 ㄹㄹ부서
2.1 ㅁㅁ부서
2.1.1 ㅅㅅ부서
2.1.1.1 ㅇㅇ부서
2.1.1.1.1 ㅊㅊ부서
위와같은 부서 정보이며 depth 는 최장 5level 까지 있고, 최단 4단계 까지 입니다.
최단 4level 부서의 경우 5level 이 null 이기 때문에 4level 부서 정보를 5level에 채워 줘야 합니다.
최단 4level 부서의 경우 5level 이 null 이기 때문에 3level 부서 정보를 4level에 , 4level 부서 정보를 5level에 채워 줘야 합니다.
고민 끝에 아래와 같은 쿼리를 작성했으니 왠지 깔끔해 보이지 않고 찜찜한 느낌이네요
아래 쿼리를 좀더 깔끔하게 수정 할 방법이 있을까요?
TN_CMN_DEP 부서정보 테이블
TN_CMN_DEP.DEP_CD 부서코드
TN_CMN_DEP.PRT_ORG_CD 상위부서코드
원하는 결과는
전체부서명, LEVEL1, LEVEL2, LEVEL3, LEVEL4, LEVEL5
ㄱㄱ부서 ㄴㄴ부서 ㄷㄷ부서 ㅈㅈ부서 1 1.1 1.1.1 1.1.1 1.1.1.1
ㄹㄹ부서 ㅁㅁ부서 ㅅㅅ부서 ㅇㅇ부서 ㅊㅊ부서 2 2.1 2.1.1 2.1.1.1 2.1.1.1.1
조언좀 부탁드립니다.
SELECT FULL_NM, DEP_CD, DEP_NM, PRT_ORG_CD, LV, CBI, (SELECT DEP_CD FROM TN_CMN_DEP WHERE LEVEL = DECODE(LV, 5, 4, 4, 3, 3, 2) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DC2, (SELECT DEP_NM FROM TN_CMN_DEP WHERE LEVEL = DECODE(LV, 5, 4, 4, 3, 3, 2) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DN2, (SELECT DEP_CD FROM TN_CMN_DEP WHERE LEVEL = DECODE(LV, 5, 3, 4, 2, 3, 1) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DC3, (SELECT DEP_NM FROM TN_CMN_DEP WHERE LEVEL = DECODE(LV, 5, 3, 4, 2, 3, 1) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DN3, (SELECT DEP_CD FROM TN_CMN_DEP WHERE LEVEL = DECODE(CBI, 0, DECODE(LV, 5, 2, 4, 1), 1, DECODE(LV, 5, 2, 4, 2)) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DC4, (SELECT DEP_NM FROM TN_CMN_DEP WHERE LEVEL = DECODE(CBI, 0, DECODE(LV, 5, 2, 4, 1), 1, DECODE(LV, 5, 2, 4, 2)) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DN4, (SELECT DEP_CD FROM TN_CMN_DEP WHERE LEVEL = DECODE(CBI, 1, DECODE(LV, 5, 1, 4, 1)) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DC5, (SELECT DEP_NM FROM TN_CMN_DEP WHERE LEVEL = DECODE(CBI, 1, DECODE(LV, 5, 1, 4, 1)) START WITH DEP_CD = A.DEP_CD CONNECT BY DEP_CD = PRIOR PRT_ORG_CD) DN5 FROM ( SELECT 'ㅇㅇ그룹' || SUBSTR(H_NM, INSTR(H_NM, ' ', 8)) FULL_NM, DEP_CD, DEP_NM, PRT_ORG_CD, LV, CBI FROM ( SELECT SUBSTR(SYS_CONNECT_BY_PATH(DEP_NM, ' '), 2) H_NM, DEP_CD, DEP_NM, PRT_ORG_CD, LEVEL LV, CONNECT_BY_ISLEAF CBI FROM TN_CMN_DEP WHERE LEVEL > 2 START WITH PRT_ORG_CD IS NULL CONNECT BY PRIOR DEP_CD = PRT_ORG_CD ORDER SIBLINGS BY PRT_ORG_CD ) ) A
WITH tn_cmn_dep AS ( SELECT '1' dep_cd, 'ㄱㄱ부서' dep_nm, '' prt_org_cd FROM dual UNION ALL SELECT '1.1' , 'ㄴㄴ부서', '1' FROM dual UNION ALL SELECT '1.1.1' , 'ㄷㄷ부서', '1.1' FROM dual UNION ALL SELECT '1.1.1.1' , 'ㅈㅈ부서', '1.1.1' FROM dual UNION ALL SELECT '2' , 'ㄹㄹ부서', '' FROM dual UNION ALL SELECT '2.1' , 'ㅁㅁ부서', '2' FROM dual UNION ALL SELECT '2.1.1' , 'ㅅㅅ부서', '2.1' FROM dual UNION ALL SELECT '2.1.1.1' , 'ㅇㅇ부서', '2.1.1' FROM dual UNION ALL SELECT '2.1.1.1.1', 'ㅊㅊ부서', '2.1.1.1' FROM dual ) SELECT nm , REGEXP_SUBSTR(cd, '[^ ]+', 1, 1) lv1 , REGEXP_SUBSTR(cd, '[^ ]+', 1, 2) lv2 , REGEXP_SUBSTR(cd, '[^ ]+', 1, 3) lv3 , REGEXP_SUBSTR(cd, '[^ ]+', 1, 4-x) lv4 , REGEXP_SUBSTR(cd, '[^ ]+', 1, 5-x) lv5 FROM (SELECT dep_cd, dep_nm, prt_org_cd , SYS_CONNECT_BY_PATH(dep_cd, ' ') cd , SYS_CONNECT_BY_PATH(dep_nm, ' ') nm , CASE WHEN LEVEL = 4 AND CONNECT_BY_ISLEAF = 1 THEN 1 ELSE 0 END x FROM tn_cmn_dep START WITH prt_org_cd IS NULL CONNECT BY PRIOR dep_cd = prt_org_cd ) ;