문제

Postgres로 번역 해야하는 NOCYCLE 절이있는 Oracle 쿼리가 있습니다.

SELECT FG_ID,CONNECT_BY_ROOT FG_ID as Parent_ID  
FROM FG t
START WITH t.Parent_filter_group_id is null 
CONNECT BY NOCYCLE PRIOR t.FILTER_GROUP_ID = t.PARENT_FILTER_GROUP_ID 
.

나는 이것을 질문의 도움으로 개조하고 답변을 겪었습니다. connect_by_root in postgres

with recursive fg_tree as (
select FG_ID,
       FG_ID as fg
from  FG
where Parent_filter_group_id is null 

union all 
select c.FG_ID,
p.fg
from FG c join fg_tree p on p.FG_ID = PARENT_FILTER_GROUP_ID
)
select * from fg_tree
order by FG_ID
.

그러나이 경우 부모가 아이들 중 하나이면 NOCYCLE에 대해서는 NON 조항이 없습니다.이 쿼리는 오류를 반환합니다.

도움이 되었습니까?

해결책

각 레벨의 ID를 수집 한 다음 "현재"ID가 경로에 포함되어 있지 않은 상태에서 조인 할 수 있습니다.

with recursive fg_tree as (
  select FG_ID,
         FG_ID as fg, 
         array[fg_id] as path
  from  FG
  where Parent_filter_group_id is null 

  union all 

  select c.FG_ID,
         p.fg, 
         p.fg||c.fg_id
  from FG c 
    join fg_tree p on p.FG_ID and c.fg_id <> ALL (p.path)
)
select fg_id, fg 
from fg_tree
order by filter_group_id
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top