I have a classstructure
table:
create table classstructure (classstructureid number(8,0), classificationid varchar2(25), parent number(8,0));
insert into classstructure(classstructureid, classificationid, parent) values(1001, 'FLEET', null);
insert into classstructure(classstructureid, classificationid, parent) values(1002, 'LIGHTDUTYVEHICLE', 1001);
insert into classstructure(classstructureid, classificationid, parent) values(1004, 'MEDIUMDUTYVEHICLE', 1001);
insert into classstructure(classstructureid, classificationid, parent) values(1022, 'ACTIVETRANSPORTATION', null);
insert into classstructure(classstructureid, classificationid, parent) values(1023, 'FACILITYWALKWAY', 1022);
insert into classstructure(classstructureid, classificationid, parent) values(1024, 'TRAIL', 1022);
insert into classstructure(classstructureid, classificationid, parent) values(1085, 'SIDEWALK', 1022);
insert into classstructure(classstructureid, classificationid, parent) values(1091, 'SDWRAMP', 1085);
commit;
select * from classstructure;
CLASSSTRUCTUREID CLASSIFICATIONID PARENT
---------------- ------------------------- ----------
1001 FLEET
1002 LIGHTDUTYVEHICLE 1001
1004 MEDIUMDUTYVEHICLE 1001
1022 ACTIVETRANSPORTATION
1023 FACILITYWALKWAY 1022
1024 TRAIL 1022
1085 SIDEWALK 1022
1091 SDWRAMP 1085
I would like to collapse the records down to hierarchy paths:
HIERARCHYPATH
---------------------------
FLEET \ LIGHTDUTYVEHICLE
FLEET \ MEDIUMDUTYVEHICLE
ACTIVETRANSPORTATION \ FACILITYWALKWAY
ACTIVETRANSPORTATION \ TRAIL
ACTIVETRANSPORTATION \ SIDEWALK
ACTIVETRANSPORTATION \ SIDEWALK \ SDWRAMP
How can I do this?
You can use sys_connect_by_path()
( since Oracle 10g Release 2
) function :
select ltrim(sys_connect_by_path(classificationid, ' \ '),' \ ') as hierarchypath
from classstructure c
where parent is not null
start with parent is null
connect by prior classstructureid = parent;
HIERARCHYPATH
--------------------------------------
FLEET \ LIGHTDUTYVEHICLE
FLEET \ MEDIUMDUTYVEHICLE
ACTIVETRANSPORTATION \ FACILITYWALKWAY
ACTIVETRANSPORTATION \ TRAIL
ACTIVETRANSPORTATION \ SIDEWALK
ACTIVETRANSPORTATION \ SIDEWALK \ SDWRAMP