You traverse hierarchical structure in reverse direction, simply use connect_by_root()
operator to get column values of the root row.
clear screen;
column IdPath format a11;
column TypeIdPathformat a11
column NamePath format a35;
with t1(id1, parent_id, type_id, Name1) as(
select 1, null, 10, 'TopParent' from dual union all
select 2, 1 , 11, 'Level1Child' from dual union all
select 3, 2 , 11, 'Level2Child' from dual
)
select connect_by_root(id1) as id1
, connect_by_root(parent_id) as ParentId
, connect_by_root(type_id) as Typeid
, connect_by_root(name1) as name1
, sys_connect_by_path(id1, '/') as IdPath
, sys_connect_by_path(type_id, '/') as TypeIdPath
, sys_connect_by_path(name1, '/') as NamePath
from t1
where connect_by_isleaf = 1
start with id1 = 3
connect by id1 = prior parent_id
Result:
id1 ParentId TypeId Name1 IdPath TypeIdPath NamePath
---------------------------------------------------------------------------
3 2 11 Level2Child /3/2/1 /11/11/10 /Level2Child/Level1Child/TopParent
Edit #1
One way to get the desired output is to use scalar sub-query:
with Locations(Id1, ParentId, Name1) as(
select 100, null, 'TopLocation' from dual union all
select 101, 100 , 'Level1Location' from dual union all
select 102, 101 , 'Level2Location' from dual
),
elements(id1, LocationId, parent_id, type_id, Name1) as(
select 1, 102, null, 10, 'TopParent' from dual union all
select 2, null, 1 , 11, 'Level1Child' from dual union all
select 3, null, 2 , 11, 'Level2Child' from dual
)
select e.*
, (select sys_connect_by_path(l.id1, '/')
from locations l
where connect_by_isleaf = 1
start with l.id1 = e.locationid
connect by l.id1 = prior parentid) as LocIdPath
, (select sys_connect_by_path(l.name1, '/')
from locations l
where connect_by_isleaf = 1
start with l.id1 = e.locationid
connect by l.id1 = prior parentid) as LocNamePath
from ( select connect_by_root(id1) as id1
, connect_by_root(parent_id) as ParentId
, connect_by_root(type_id) as Typeid
, connect_by_root(name1) as name1
, sys_connect_by_path(id1, '/') as IdPath
, sys_connect_by_path(type_id, '/') as TypeIdPath
, sys_connect_by_path(name1, '/') as NamePath
, locationid
from elements
where connect_by_isleaf = 1
start with id1 = 3
connect by id1 = prior parent_id ) e
Result:
ID1 PARENTID TYPEID NAME1 IDPATH TYPEIDPATH NAMEPATH LOCATIONID LOCIDPATH LOCNAMEPATH
---------- ---------- ----------- ----------- ----------- ----------------------------------- ---------- ------------- -------------------------------------------
3 2 11 Level2Child /3/2/1 /11/11/10 /Level2Child/Level1Child/TopParent 102 /102/101/100 /Level2Location/Level1Location/TopLocation