• 분류 전체보기 (160)
    • 개인프로젝트 (5)
      • 시연영상모음 (4)
      • 주저리주저리.. (1)
    • 자바스크립트이야기 (69)
      • ExtJS (30)
      • ExtJS 유의사항 (3)
      • HTML5 (1)
      • jQuery (17)
      • jQuery플러그인소개 (9)
      • jQuery UI 소개 (9)
    • 스프링연동하기 (23)
      • spring3 mvc 설정 (4)
      • spring3 mybatis 설정 (4)
      • spring3 기타설정 (11)
      • ibatis and mybatis (4)
    • DB (26)
      • MySQL vs MS-SQL vs O.. (5)
      • MySQL (10)
      • MSSQL (5)
      • Oracle (6)
    • 서버에웹환경구축하기 (13)
      • 1.VirtualBox+CentOS .. (5)
      • 2.JAVA(JDK) 설치 (1)
      • 3.Apache+Tomcat 설치 (3)
      • 4,SVN Server 설치 (1)
      • 5.마리아DB(MariaDB) 설치 (2)
      • 6.몽고DB(MongoDB)설치 (1)
    • 샘플소스 (14)
      • 샘플소스(JAVA) (7)
      • 샘플소스(JSTL) (4)
      • 샘플소스(jQuery/스크립트) (3)
    • 에디터연동 (5)
      • NAVER-스마트에디터 (4)
      • DAUM-다음에디터 (1)
    • 블로그팁 (3)
    • 디지털이야기 (2)
댓글
/139
2014. 8. 12. 00:30
오라클 계층형 트리구조 쿼리 (재귀호출) - START WITH .. CONNECT BY PRIOR


2014/08/12 - [DB/MSSQL] - MS-SQL 계층형 트리구조 쿼리 (재귀호출) - with ...(col 1, col 2 ...,col n) as ... union .


이번 포스팅은 계층형 트리구조 쿼리를 이요하여 트리구조 출력을 하려한다.


오라클에서 계층형 트리는


START WITH .. CONNECT BY PRIOR 구문을 이용하여 트리구조를 심플하게 출력할 수 있다.


데이터 예를 들도록 해보자


1. 테이블 생성

create table tree_table(
	id number not null,
    parent_id number not null,
    name nvarchar2(30) not null
);

2. 트리쿼리 출력을 위한 임의의 데이터 생성

-- 1depth 
insert into tree_table(id,parent_id,name)
values(1,0,'1depth 1');
insert into tree_table(id,parent_id,name)
values(2,0,'1depth 2');
insert into tree_table(id,parent_id,name)
values(3,0,'1depth 3');

-- 2depth 1
insert into tree_table(id,parent_id,name)
values(4,1,'2depth 1-1');
insert into tree_table(id,parent_id,name)
values(5,1,'2depth 1-2');
insert into tree_table(id,parent_id,name)
values(6,1,'2depth 1-3');

-- 2depth 2
insert into tree_table(id,parent_id,name)
values(7,2,'2depth 2-1');
insert into tree_table(id,parent_id,name)
values(8,2,'2depth 2-2');
insert into tree_table(id,parent_id,name)
values(9,2,'2depth 2-3');

-- 3depth 1
insert into tree_table(id,parent_id,name)
values(10,4,'3depth 1-1');
insert into tree_table(id,parent_id,name)
values(11,4,'3depth 1-2');
insert into tree_table(id,parent_id,name)
values(12,4,'3depth 1-3');

3. 재귀쿼리로 트리목록 출력

SELECT id,parent_id,name, LTRIM (SYS_CONNECT_BY_PATH (name, ' > '), ' > ') AS depth_fullname
 FROM tree_table 
 START WITH parent_id = 0
 CONNECT BY PRIOR id=parent_id

위의 재귀쿼리 실행결과를 확인해 보도록 하자 






※ ORACLE 11g 에서 ms-sql 2005에서 제공되는 재귀쿼리가 동일하게 동작된다. 

    다음과 같이 쿼리문을 주어서 실행 하더라도 같은 결과가 출력된다.


WITH tree_query(id,parent_id,name,sort, depth_fullname)  AS 
(
  SELECT  
         id
       , parent_id
       , name
       , ''||id as sort
       , ''||name as depth_fullname
    FROM tree_table
    WHERE parent_id = 0
    UNION ALL 
    SELECT
          B.id
        , B.parent_id
        , B.name
        , C.sort || ' > ' || B.id as sort
        , C.depth_fullname || ' > ' || B.name as  depth_fullname
    FROM  tree_table B, tree_query C
    WHERE B.parent_id = C.id
) 
SELECT id, parent_id, name,depth_fullname FROM tree_query order by sort



위와같이 트리구조로 잘 조회되었다.


   1depth 1

   |

   -------- 2depth 1-1

   |                 |

   |                 ---- 3depth 1-1

   |                 |

   |                 ---- 3depth 1-2

   |                 |

   |                 ---- 3depth 1-3

   -------- 2depth 1-2

   |

   -------- 2depth 1-3

   |

   1depth 2

   |

   -------- 2depth 2-1

   |

   -------- 2depth 2-2

   |

   -------- 2depth 2-2

   |   

   1depth 3


위와같은 구조로 출력된 셈이다.

잘 활용해서 실무에 적용해보도록 합시다~ 파이팅

   


슈퍼맨슈퍼맨슈퍼맨



도움이 되셨다면 공감클릭! 궁금하신점은 댓글!!

저작자표시 (새창열림)

'DB > Oracle' 카테고리의 다른 글

Oracle - if ~ else 조건문 사용하기 : DECODE  (0) 2014.08.24
오라클 - 널체크(NVL 사용하기)  (0) 2014.08.23
ORACLE 오라클 rownum을 이용하여 페이징 처리하기  (0) 2014.08.15
오라클(Oracle) 데이터를 일괄로 다른테이블에 INSERT 하기 (INSERT INTO ... SELECT ... FROM ...)  (2) 2014.08.13
오라클(Oracle) sequence 생성, 등록, 최종 시퀀스값 조회하기  (2) 2014.08.11

티스토리툴바