Oracel递归统计节点叶子节点
需求:
数据库表中直接根据根节点的id统计它有多少叶子节点(即最底层的节点)
数据库表中直接根据根节点的id统计它有多少子节点(包括子节点和子节点的子节点)
SQL:
with treeNode as
(select sys_connect_by_path(label, '>') path, level deep, t.*
from tmp t
start with t.xxzjbh = '1111111'
connect by prior xxzjbh = parentId)
select t.xxzjbh,
(select count(1) from treeNode k where k.xxzjbh != t.xxzjbh start with k.xxzjbh = t.xxzjbh connect by prior xxzjbh = parentId ) zl_count,
(select count(1) from treeNode k where k.xxzjbh != t.xxzjbh start with k.xxzjbh = t.xxzjbh connect by prior xxzjbh = parentId ) bq_count
from treeNode t
where t.parentid = '1111111'