0

有路线图(连接城市列表):

drop table aaa; 
create table aaa(a varchar2(10),  b varchar2(10));
insert into aaa values ('Rome','Berlin');
insert into aaa values ('London','Rome');
insert into aaa values ('London','New-York');
insert into aaa values ('New-York','Dallas');

我需要找到路径:柏林=>罗马=>纽约=>达拉斯

变体 1:

select sys_connect_by_path(DECODE(a, PRIOR a, b, a),'=>') PATH1
from aaa
start with a = 'Berlin' or b = 'Berlin'
connect by nocycle  Prior a = b or prior b = a

返回:=>罗马=>伦敦

变体 2:

select sys_connect_by_path(DECODE(a, PRIOR a, b, a),'=>') PATH1
from aaa
start with a = 'Berlin' or b = 'Berlin'
connect by Prior a = b or prior b = a

返回:ERROR ORA-01436 CONNECT BY 用户数据循环

任何建议,如何通过分层查询获得预期结果?

4

1 回答 1

1
select 
  sys_connect_by_path(b,'=>') PATH1
from 
(
  select 
    least(a, b) a, 
    greatest(a, b) b
  from aaa
)
start with a = 0 
connect by prior b = a

更新:

select 
  sys_connect_by_path(b, '=>') PATH1
from 
  (
    select a, b from aaa
    union all
    select b, a from aaa
    union all
    select null, 'Berlin' from dual
  )
start with a is null
connect by nocycle prior b = a
于 2013-04-24T20:26:41.863 回答