user2965499
user2965499

Reputation: 29

Hierarchical query rollup Rollup

I have the following table:

parent_id   child_id    child_class
1   2   1
1   3   1
1   4   2
2   5   2
2   6   2

Parent_id represents a folder id. Child id represents either a child folder (where child_class=1) or child file (where child_class=2).

I'd like to get a rollup counter (bottom up) of all files only (child_class=2) the following way. for example if C is a leaf folder (no child folders) with 5 files, and B is a parent folder of C that has 4 files in it, the counter on C should say 5 and the counter on B should say 9 (=5 from C plus 4 files in B) and so forth recursively going bottom up taking into consideration sibling folders etc.

In the example above I expect the results below (notice 3 is a child folder with no files in it):

parent_id   FilesCounter
3   0
2   2
1   3

I prefer an SQL query for performance but function is also possible.

I tried mixing hirarchical query with rollup (sql 2008 r2) with no success so far.

Please advise.

Upvotes: 0

Views: 970

Answers (2)

user2965499
user2965499

Reputation: 29

Zak's answer was close, but the root folder did not rollup well. The following does the work:

with par_child as (
select 1 as parent_id,             2 as child_id,              1 as child_class
union all select 1,              3,              1
union all select 1,              4,              2
union all select 2,              5,              1
union all select 2,              6,              2
union all select 2,              10,           2  
union all select 3,              11,           2  
union all select 3,              7 ,             2
union all select 5,              8 ,             2
union all select 5,              9 ,             2
union all select 5,              12,           1  
union all select 5,              13,           1  
)
, child_cnt as 
(
      select parent_id as root_parent_id, parent_id, child_id, child_class, 1 as lvl from par_child    union all
      select cc.root_parent_id, pc.parent_id, pc.child_id, pc.child_class, cc.lvl + 1 as lvl from
      par_child pc join child_cnt cc on (pc.parent_id=cc.child_id)
),
distinct_folders as (
select distinct child_id as folder_id from par_child where child_class=1
)
select root_parent_id, count(child_id) as cnt from child_cnt where child_class=2 group by root_parent_id
union all
select folder_id, 0 from distinct_folders df where not exists (select 1 from par_child pc where df.folder_id=pc.parent_id)

Upvotes: 0

Zak
Zak

Reputation: 461

This CTE should do the trick... Here is the SQLFiddle.

SELECT parent_id, child_id, child_class,
(SELECT COUNT(*) FROM tbl a WHERE a.parent_id = e.parent_id AND child_class <> 1) AS child_count
INTO tbl2
FROM tbl e

;WITH CTE (parent_id, child_id, child_class, child_count)
AS
(
-- Start with leaf nodes
   SELECT parent_id, child_id, child_class, child_count 
   FROM tbl2
   WHERE child_id NOT IN (SELECT parent_id from tbl)
   UNION ALL
-- Recursively go up the chain
   SELECT e.parent_id, e.child_id, e.child_class, e.child_count + d.child_count
   FROM tbl2 e
   INNER JOIN CTE AS d
   ON e.child_id = d.parent_id
)
-- Statement that executes the CTE
SELECT FOLDERS.parent_id, max(ISNULL(child_count,0)) FilesCounter
FROM (SELECT parent_id FROM tbl2 WHERE parent_id NOT IN (select child_id from tbl2)
     UNION
     SELECT child_id FROM tbl2 WHERE child_class = 1) FOLDERS
LEFT JOIN CTE ON FOLDERS.parent_id = CTE.parent_id
GROUP BY FOLDERS.parent_id 

Upvotes: 0

Related Questions