Getting counts/totals at each level of a hierarchical query using CONNECT BY

后端 未结 2 1610
眼角桃花
眼角桃花 2021-02-06 07:39

I am having a heck of a time with this. I am trying to write a query (using Oracle), against a table with a recursive relationship (hierarchical) and get the total number of re

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 07:48

    This one is pretty straightforward:

    09:38:54 HR@vm_xe> l                                      
      1  select sys_connect_by_path(dp.dir_name, '/') path    
      2         ,(select count(file_id)                       
      3             from dirs dc                              
      4                  ,files f                             
      5            where f.dir_id(+) = dc.dir_id              
      6          connect by prior dc.dir_id = dc.parent_dir_id
      7            start with dc.dir_id = dp.dir_id           
      8          ) count                                      
      9    from dirs dp                                       
     10    connect by prior dp.dir_id = dp.parent_dir_id      
     11*   start with dp.parent_dir_id is null                
    09:38:55 HR@vm_xe> /                                      
    
    PATH                                COUNT                 
    ------------------------------ ----------                 
    /ROOT                                   6                 
    /ROOT/DIR1_1                            4                 
    /ROOT/DIR1_1/DIR2_1                     1                 
    /ROOT/DIR1_1/DIR2_2                     3                 
    /ROOT/DIR1_2                            2                 
    
    5 rows selected.                                          
    
    Elapsed: 00:00:00.02                                      
    

提交回复
热议问题