Elic - великий телепат, ни на символ не промазал, ни скобочкой не ошися. :)
SQL> with b as(
2 select 1 branchId, 0 parentId, 0 recId from dual union all
3 select 2, 0, 1 from dual union all
4 select 3, 1, 2 from dual union all
5 select 4, 1, 3 from dual union all
6 select 5, 2, 4 from dual union all
7 select 6, 2, 5 from dual union all
8 select 77, 4, 6 from dual
9 )
10 ,v as(
11 select 0 branchId, 1 Value, 0 recId from dual union all
12 select 1, 2, 1 from dual union all
13 select 4, 3, 2 from dual
14 )
15 select b.branchId,to_number(substr(rtrim(sys_connect_by_path(nvl(v1.value,v2.value), '\'), '\')
16 , instr(rtrim(sys_connect_by_path(nvl(v1.value,v2.value), '\'), '\'), '\', -1) + 1
17 )
18 ) value
19 from b,v v1,v v2
20 where b.branchId = v1.branchId(+)
21 and b.parentId = v2.branchId(+)
22 start with parentId = 0
23 connect by prior b.branchID = parentId;
BRANCHID VALUE
---------- ----------
1 2
3 2
4 3
77 3
2 1
5 1
6 1
7 rows selected