ITPub博客

首页 > 数据库 > Oracle > 数据库开发基础--层次查询+

数据库开发基础--层次查询+

原创 Oracle 作者:lff1530983327 时间:2015-03-23 14:52:51 0 删除 编辑

with bus as

 (select 'A' dstart, 'B' dend, '120' distance

    from dual

  union all

  select 'B' dstart, 'C' dend, '200' distance

    from dual

  union all

  select 'A' dstart, 'D' dend, '150' distance

    from dual

  union all

  select 'D' dstart, 'M' dend, '300' distance

    from dual

  union all

  select 'C' dstart, 'E' dend, '180' distance

    from dual

  union all

  select 'F' dstart, 'M' dend, '260' distance from dual)

select level,

       connect_by_root dstart,

       dend,

       distance,

       SYS_CONNECT_BY_PATH(dstart, '->'),

       SYS_CONNECT_BY_PATH(dstart, '->') || '->' || dend,

       lead(distance,level-1)over(partition by level order by level)

 

  from bus

 start with dstart is not null

connect by (prior dend = dstart);

 

  -----------------------------------------------

  with bus as

(

select 'A' dstart, 'B' dend, '120' distance from dual union all

select 'B' dstart, 'C' dend, '200' distance from dual union all

select 'A' dstart, 'D' dend, '150' distance from dual union all

select 'D' dstart, 'M' dend, '300' distance from dual union all

select 'C' dstart, 'E' dend, '180' distance from dual union all

select 'F' dstart, 'M' dend, '260' distance from dual 

)

select * from  bus;

  ------------------------------------------

with bus as

(

select 'A' dstart, 'B' dend, '120' distance from dual union all

select 'B' dstart, 'C' dend, '200' distance from dual union all

select 'A' dstart, 'D' dend, '150' distance from dual union all

select 'D' dstart, 'M' dend, '300' distance from dual union all

select 'C' dstart, 'E' dend, '180' distance from dual union all

select 'F' dstart, 'M' dend, '260' distance from dual 

)

select dstart, wmsys.wm_concat(dend)  from bus group by dstart

 

--------------------------

with bus as

(

select 'A' dstart, 'B' dend, '120' distance from dual union all

select 'B' dstart, 'C' dend, '200' distance from dual union all

select 'A' dstart, 'D' dend, '150' distance from dual union all

select 'D' dstart, 'M' dend, '300' distance from dual union all

select 'C' dstart, 'E' dend, '180' distance from dual union all

select 'F' dstart, 'M' dend, '260' distance from dual 

)

select dstart,dend, CONNECT_BY_ISLEAF isleaf, 

 

sys_connect_by_path(dstart, '->')||'-'||dend "Path" 

 

FROM bus 

 

START WITH dstart IS NOT NULL 

 

CONNECT BY PRIOR  dend =dstart; 

 

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30018455/viewspace-1470492/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-11-12

  • 博文量
    72
  • 访问量
    49748