ITPub博客

首页 > 数据库 > Oracle > 11GR2递归With子查询

11GR2递归With子查询

原创 Oracle 作者:壹頁書 时间:2015-12-28 11:11:44 0 删除 编辑
11GR2增加了递归With子查询
详见 剑破冰山 429页

树形遍历有了新方法.

原来使用sys_connect_by_path函数
  1. select  
  2.     employee_id,  
  3.     first_name,  
  4.     manager_id,  
  5.     level,  
  6.     sys_connect_by_path(employee_id,'\') as path,  
  7.     connect_by_root(first_name) as top_manager  
  8. from employees  
  9. start with manager_id is null   
  10. connect by prior employee_id=manager_id; 

递归With新写法.
由两个SQL union all而成.
上面的SQL是起始的条件.
下面的SQL是递归的条件.
  1. with t(employee_id,first_name,manager_id,the_level,path,top_manager) as   
  2. (  
  3.     select   
  4.         employee_id,  
  5.         first_name,  
  6.         manager_id,  
  7.         1 as the_level,  
  8.         '\'||employee_id as path,  
  9.         first_name as top_manager  
  10.     from employees   
  11.     where manager_id is null  
  12.     union all  
  13.     select   
  14.         e.employee_id,  
  15.         e.first_name,  
  16.         e.manager_id,  
  17.         1+t.the_level as the_level,  
  18.         t.path||'\'||e.employee_id as path,  
  19.         t.top_manager  
  20.     from t,employees e  
  21.     where t.employee_id=e.manager_id  
  22. )  
  23. select * from t;  

搜索子句
SEARCH
①   { DEPTH FIRST BY c_alias [, c_alias]...
        [ ASC | DESC ]
        [ NULLS FIRST | NULLS LAST ]
②   | BREADTH FIRST BY c_alias [, c_alias]...
        [ ASC | DESC ]
        [ NULLS FIRST | NULLS LAST ]
     }
③    SET ordering_column

注:
①DEPTH FIRST表示按深度优先的顺序来输出。BY后面的列名及其升降序、空值放置顺序指明了在深度优先的前提下,同一层次的数据的排序情况。
    这和原来CONNECT BY查询中的ORDER SIBLING BY子句是一样的。
②BREADTH FIRST表示按广度优先的顺序来输出。BY后面的列名及其升降序、空值放置顺序指明了在广度优先的前提下,同一层次的数据的排序情况。
③列名ordering_column用于存放排序后的序号,是一个从1开始的连续正整数。后续的查询中可以利用这个列得知某行数据在整个结果集中的位置。

sys_connect_by_path结果:


深度优先遍历
  1. with t(employee_id,first_name,manager_id,the_level,path,top_manager) as   
  2. (  
  3.     select   
  4.         employee_id,  
  5.         first_name,  
  6.         manager_id,  
  7.         1 as the_level,  
  8.         '\'||employee_id as path,  
  9.         first_name as top_manager  
  10.     from employees   
  11.     where manager_id is null  
  12.     union all  
  13.     select   
  14.         e.employee_id,  
  15.         e.first_name,  
  16.         e.manager_id,  
  17.         1+t.the_level as the_level,  
  18.         t.path||'\'||e.employee_id as path,  
  19.         t.top_manager  
  20.     from t,employees e  
  21.     where t.employee_id=e.manager_id  
  22. )  
  23. search depth first by manager_id set seq  
  24. select * from t;  


广度优先遍历
  1. with t(employee_id,first_name,manager_id,the_level,path,top_manager) as   
  2. (  
  3.     select   
  4.         employee_id,  
  5.         first_name,  
  6.         manager_id,  
  7.         1 as the_level,  
  8.         '\'||employee_id as path,  
  9.         first_name as top_manager  
  10.     from employees   
  11.     where manager_id is null  
  12.     union all  
  13.     select   
  14.         e.employee_id,  
  15.         e.first_name,  
  16.         e.manager_id,  
  17.         1+t.the_level as the_level,  
  18.         t.path||'\'||e.employee_id as path,  
  19.         t.top_manager  
  20.     from t,employees e  
  21.     where t.employee_id=e.manager_id  
  22. )  
  23. search BREADTH first by manager_id set seq  
  24. select * from t;  


参考:
http://www.oracle.com/technetwork/cn/articles/hartley-recursive-086819-zhs.html

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

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

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339963