如何使用postgresql中的nocycle -connect启动

问题描述 投票:-2回答:1
SELECT coalesce(RPAD('IN',16,' ') || RPAD( M.CASETYPE||'/' || M.CASENUMBER || '/' || M.CASEYEAR,16, ' '),' ') as inmatter
FROM (
  select  level as LEV, l1.LINKCASECCIN as CCI ,l1.linkcategorycode as lcode
  from hclive.LINKEDMATTERS l1
  start with l1.MAINCASECCIN ='001003201400100' and  l1.linkcategorycode='I'
  connect by nocycle prior l1.LINKCASECCIN = l1.MAINCASECCIN  and l1.linkcategorycode = 'I'
) s1, 
  hclive.MAIN M
where M.CCIN=CCI
postgresql
1个回答
2
投票

connect by是在Postgres用recursive common table expression完成的。

with recursive tree as (
  select 1 as level, l1.linkcaseccin as cci, l1.linkcategorycode as lcode
  from hclive.linkedmatters l1
  where l1.maincaseccin ='001003201400100' 
    and l1.linkcategorycode='I'

  union all

  select p.level + 1, c1.linkcaseccin as cci, c1.linkcategorycode as lcode
  from hclive.linkedmatters c1
    join tree p on p.maincaseccin = c1.linkcaseccin 
  where c1.linkcategorycode='I'
)
select coalesce(rpad('IN',16,' ') || rpad( m.casetype||'/' || m.casenumber || '/' || m.caseyear,16, ' '),' ') as inmatter
from tree t 
 join hclive.main M on m.ccin = t.cci;

level列似乎没有必要,因为您在查询中根本不使用它,但我留在那里作为示例在Postgres中获取相同的信息。

© www.soinside.com 2019 - 2024. All rights reserved.