如何在层次结构树的每个节点中进行 SQL 分组查询?
How to make SQL query with grouping in each node of an hierarchy tree?
有一个 table 具有通常的“子到父”结构,其中每个节点都有权重。
TREE_TABLE
----------------------------------------
id | parent_id | name | weight
----------------------------------------
1 | NULL | N1 | 51
2 | 1 | N12 | 62
3 | 1 | N13 | 73
4 | 2 | N124 | 84
5 | 2 | N125 | 95
// for convenience the "name" column shows path from a node to the root node
如何构建一个 SQL 查询以生成一组行,其中每行代表每个特定节点及其子节点的分组?
请根据您的选择使用任何 SQL 方言。只需要一个大概的想法或解决方案的原型。
为了解决这个问题,我正在试验 GROUPING SETS 和 ROLLUP 报告,但无法弄清楚处理“动态”数量的分组级别的方法。
预期查询结果示例:
RESULT
--------------------------
name | summary_weight
--------------------------
N1 | (51+62+73+84+95)
N12 | (62+84+95)
N124 | (84)
N125 | (95)
N13 | (73)
例如:
with
datum(id,parent_id,name,weight)
as
(
select 1,NULL,'N1',51 from dual union all
select 2 , 1 , 'N12' , 62 from dual union all
select 3 , 1 , 'N13' , 73 from dual union all
select 4 , 2 , 'N124' , 84 from dual union all
select 5 , 2 , 'N125' , 95 from dual
),
step1 as
(
select id,parent_id,name,weight, connect_by_root name root,connect_by_isleaf is_parent
from datum
connect by prior id = parent_id
)
select root,sum(weight) sum_w,
'('||listagg(weight,'+') within group(order by null) ||')' str_w,
'('||listagg(name,'+') within group(order by null) ||')' str_n
from step1
group by root
order by 1;
link: Hierarchical Queries
如果你想SUM
起来,你可以使用这个选项:
SELECT
name,
(SELECT
sum(t2.weight)
FROM tree t2
WHERE t2.name LIKE t1.name || '%' )
FROM tree t1
ORDER BY rpad(name,10,'0');
如果你想连接文本,你可以使用这个:
SELECT
name,
(SELECT
'(' || string_agg(t2.weight || '','+') || ')'
FROM tree t2
WHERE t2.name LIKE t1.name || '%' )
FROM tree t1
ORDER BY rpad(name,10,'0');
有一个 table 具有通常的“子到父”结构,其中每个节点都有权重。
TREE_TABLE
----------------------------------------
id | parent_id | name | weight
----------------------------------------
1 | NULL | N1 | 51
2 | 1 | N12 | 62
3 | 1 | N13 | 73
4 | 2 | N124 | 84
5 | 2 | N125 | 95
// for convenience the "name" column shows path from a node to the root node
如何构建一个 SQL 查询以生成一组行,其中每行代表每个特定节点及其子节点的分组?
请根据您的选择使用任何 SQL 方言。只需要一个大概的想法或解决方案的原型。
为了解决这个问题,我正在试验 GROUPING SETS 和 ROLLUP 报告,但无法弄清楚处理“动态”数量的分组级别的方法。
预期查询结果示例:
RESULT
--------------------------
name | summary_weight
--------------------------
N1 | (51+62+73+84+95)
N12 | (62+84+95)
N124 | (84)
N125 | (95)
N13 | (73)
例如:
with
datum(id,parent_id,name,weight)
as
(
select 1,NULL,'N1',51 from dual union all
select 2 , 1 , 'N12' , 62 from dual union all
select 3 , 1 , 'N13' , 73 from dual union all
select 4 , 2 , 'N124' , 84 from dual union all
select 5 , 2 , 'N125' , 95 from dual
),
step1 as
(
select id,parent_id,name,weight, connect_by_root name root,connect_by_isleaf is_parent
from datum
connect by prior id = parent_id
)
select root,sum(weight) sum_w,
'('||listagg(weight,'+') within group(order by null) ||')' str_w,
'('||listagg(name,'+') within group(order by null) ||')' str_n
from step1
group by root
order by 1;
link: Hierarchical Queries
如果你想SUM
起来,你可以使用这个选项:
SELECT
name,
(SELECT
sum(t2.weight)
FROM tree t2
WHERE t2.name LIKE t1.name || '%' )
FROM tree t1
ORDER BY rpad(name,10,'0');
如果你想连接文本,你可以使用这个:
SELECT
name,
(SELECT
'(' || string_agg(t2.weight || '','+') || ')'
FROM tree t2
WHERE t2.name LIKE t1.name || '%' )
FROM tree t1
ORDER BY rpad(name,10,'0');