Tsql 分层查询汇总

Tsql 分层查询汇总,tsql,hierarchical,rollup,Tsql,Hierarchical,Rollup,我有下表: parent_id child_id child_class 1 2 1 1 3 1 1 4 2 2 5 2 2 6 2 父级\u id表示文件夹id。子级id表示子级为1的子文件夹或子级为2的子文件 我想用下面的方法得到一个所有文件的自底向上的汇总计数器only child_class=2。例如,如果C是一个叶文件夹,没有包含5个文件的子文件夹,B是一个包含4个文件的C的父文件夹,C上的计数器应为5,B上的计数器应为9=5,从C加

我有下表:

parent_id   child_id    child_class
1   2   1
1   3   1
1   4   2
2   5   2
2   6   2
父级\u id表示文件夹id。子级id表示子级为1的子文件夹或子级为2的子文件

我想用下面的方法得到一个所有文件的自底向上的汇总计数器only child_class=2。例如,如果C是一个叶文件夹,没有包含5个文件的子文件夹,B是一个包含4个文件的C的父文件夹,C上的计数器应为5,B上的计数器应为9=5,从C加上B中的4个文件,以此类推,考虑到兄弟文件夹等,递归自下而上

在上面的示例中,我希望得到以下结果注意3是一个没有文件的子文件夹:

parent_id   FilesCounter
3   0
2   2
1   3
我更喜欢SQL查询的性能,但函数也是可能的

我尝试将Hirchical查询与rollup sql 2008 r2混合使用,但迄今为止没有成功


请告知。

这个CTE应该可以做到这一点。。。这是你的电话号码


扎克的回答很接近,但根文件夹没有很好地汇总。以下工作:

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)
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)