설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 94건, 최근 0 건
   

SYS_CONNECT_BY_PATH

글쓴이 : 꼬마로봇 날짜 : 2014-10-02 (목) 17:08 조회 : 11442

sys_connect_by_path

계층 쿼리를 사용하여 상위 계층과 하위 계층을 결합할때 /이나 ,등의 구분자를 사용하여 상,하위 계층을 구분하여 나타낸다.

1. 오라클 구문 예

구문

SQL> select id,subid, Level, ltrim(sys_connect_by_path (to_char (id), ','), ',') as Path
from sys_cbp_test
start with subid is null
connect by prior id=subid;


2. PPAS 구문 예

PPAS의 경우 connect by prior 등과 같은 계층 쿼리를 사용가능하나, sys_conect_by_path 함수를 지원하지 않기 때문에 CTE 쿼리를 이용하여 다음과 같이 변경해야한다.

구문

with recursive X (id, subid, ArrPath) as
(
select id, subid, array [id]
from sys_cbp_test
where subid is null
union all
select b.id, b.subid, X.ArrPath || b.id

from X, sys_cbp_test b
where X.id = b.subid
)
select id,subid,array_upper (ArrPath, 1) as Level, Array_to_string (ArrPath, ',') as path
from X
order by ArrPath;

 

결과값

 id | oyaid | level |   path
----+-------+-------+-----------
  1 |       |     1 | 1
  2 |     1 |     2 | 1,2
  3 |     2 |     3 | 1,2,3
  4 |     3 |     4 | 1,2,3,4
  7 |     2 |     3 | 1,2,7
  5 |     1 |     2 | 1,5
  6 |     5 |     3 | 1,5,6
 20 |       |     1 | 20
 21 |    20 |     2 | 20,21
 22 |    21 |     3 | 20,21,22
(10 rows)

 


   

postgresdba.com