계층구조 문의 0 2 1,561

by 세븐블랙홀 [SQL Query] 계층구조 oracle query [2017.04.03 10:56:42]


with t as
(
select 1 group_id, '1001' code, ''     up_code from dual union all
select 1 group_id, '3001' code, '1001' up_code from dual union all
select 1 group_id, '3002' code, '1001' up_code from dual union all
select 1 group_id, '5001' code, '3001' up_code from dual union all
select 1 group_id, '5002' code, '3002' up_code from dual union all
select 2 group_id, '1001' code, ''     up_code from dual union all
select 2 group_id, '3001' code, '1001' up_code from dual union all
select 2 group_id, '3002' code, '1001' up_code from dual union all
select 2 group_id, '5001' code, '3001' up_code from dual union all
select 2 group_id, '5001' code, '3002' up_code from dual
)
select *
  from t
 where 1 = 1
   and group_id = 1
 start with t.up_code is null
connect by prior t.code = t.up_code
;

위의 Data는 1 Group 과 2Group 이 같은Data를 가지고 있는데 
그룹별로 계층구조를 나열하고 싶습니다.

1    1001    
1    3001    1001
1    5001    3001
1    3002    1001
1    5002    3002
2    1001    
2    3001    1001
2    5001    3001
2    3002    1001
2    5001    3002

가능한가요?

by 마농 [2017.04.03 11:21:08]
SELECT *
  FROM t
 START WITH up_code IS NULL
 CONNECT BY PRIOR code     = up_code
        AND PRIOR group_id = group_id
;

 


by 세븐블랙홀 [2017.04.03 11:33:04]

감사합니다.~~ ^^

and 조건을 저렇게 넣는 거네요~~

언제나 많은 가르침 감사합니다~

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입