by DDCC [SQL Query] 계층 구조 sql 튜닝 [2018.05.15 11:57:26]
계층구조관련 sql 문의 드립니다.
## 계층구조 ##
00000 +-10000 | +-10100 | | +-10110 | | | +-10111 | | | +-10112 | | +-10120 | | +-10122 | | +-10122 | +-10200 | | +-10210 | | +-10220 | +-10300 | +-20000 +-20100 +-20200
위와 같은 계층 구조에서
특정 값 "did: 10100"을 기준으로
- 상위: 00000, 10000
- 하위: 10110, 10200, 10111, 10112, 10122, 10122
를 한꺼번에출력하고자 아래와 같은 SQL을 작성하였습니다.
이 방법 말고 더 간단히 출력할 수 있는 쿼리가 있을까요?
도움 부탁 드립니다.
WITH t AS ( select '00000' did, null pid from dual union all select '10000' did, '00000' pid from dual union all select '10100' did, '10000' pid from dual union all select '10200' did, '10000' pid from dual union all select '10300' did, '10000' pid from dual union all select '10110' did, '10100' pid from dual union all select '10120' did, '10100' pid from dual union all select '10111' did, '10110' pid from dual union all select '10112' did, '10110' pid from dual union all select '10121' did, '10120' pid from dual union all select '10122' did, '10120' pid from dual union all select '20000' did, '00000' pid from dual union all select '20100' did, '20000' pid from dual union all select '20200' did, '20000' pid from dual ) SELECT did, pid FROM t START WITH did = '10100' CONNECT BY did = PRIOR pid union SELECT did, pid FROM t START WITH did = '10100' CONNECT BY PRIOR did = pid order by did ;