oracle树形查询,根据第一级作为条件,查询之下所有数据并根据第二级进行汇总统计。通过oracle的start with connect ty prior 查询第二级,再将第二级作为条件,进行树形查询,此时可以通过connect_by_root(id)将第二级查询出来,进行group by汇总统计。
select id, mc, connect_by_isleaf leaf, sys_connect_by_path(id,'|') path, connect_by_root(id) secondid, level from table1 t1
where start with t1.id in (select id from table t2 where level =2 start with id = '123' connect by prior t2.id = t2.parentid)
connect by prior t1.id = t1.parentid;
select id, mc, connect_by_isleaf leaf, sys_connect_by_path(id,'|') path, connect_by_root(id) secondid, level from table1 t1
where start with t1.id in (select id from table t2 where level =2 start with id = '123' connect by prior t2.id = t2.parentid)
connect by prior t1.id = t1.parentid;