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

后端 未结 2 1608
眼角桃花
眼角桃花 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 08:08

    select sys_connect_by_path(D.dir_name, '/'), S.count_distinct_file_id
    from DIRS D
    inner join (select subtree_root_dir_id
                , count(distinct file_id) count_distinct_file_id
            from (select distinct connect_by_root D.DIR_ID subtree_root_dir_id
                        , F.file_id 
                    from DIRS D
                    left outer join FILES F on F.dir_id = D.dir_id
                    start with 1=1 connect by prior D.dir_id = D.parent_dir_id)
            group by subtree_root_dir_id) S
        on D.dir_id = S.subtree_root_dir_id
    start with D.dir_id = 1 connect by prior D.dir_id = D.parent_dir_id
    

    Gives the results you asked for, but my gut says I am not seeing something, and that the query can be much simpler. (Please do not accept this answer until a few days have passed, in hopes that some one submits a better answer.)

提交回复
热议问题