如何在postgresql中使用nocycle的-connect开头

时间:2015-02-15 15:00:47

标签: postgresql

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

1 个答案:

答案 0 :(得分:2)

在Postgres中使用recursive common table expression完成了

connect by

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中获取相同的信息。