select t1.id, t1.name, sum(total)
from (select t1.id, connect_by_root(t1.name) name, total
from tab t1
connect by prior t1.id = t1.pid) t1
group by t1.id, t1.name;
select id, name, sum(total) from
(select connect_by_root(a.id) id,connect_by_root(a.name) name, total from tab a connect by prior id = pid)
group by id, name
order by 1;