计算层次结构路径的顶层?
Calculate top-level of hierarchy path?
我有一个 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
我想计算每一行层次结构路径的顶层:
CLASSSTRUCTUREID TOP LEVEL
---------------- ---------
1001 FLEET
1002 FLEET
1004 FLEET
1022 ACTIVETRANSPORTATION
1023 ACTIVETRANSPORTATION
1024 ACTIVETRANSPORTATION
1085 ACTIVETRANSPORTATION
1091 ACTIVETRANSPORTATION
是否可以使用 Oracle 18c SQL 执行此操作?
您可以使用 hierarchical query, with connect_by_root
:
select classstructureid, connect_by_root(classificationid)
from classstructure
connect by parent = prior classstructureid
start with parent is null;
我有一个 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
我想计算每一行层次结构路径的顶层:
CLASSSTRUCTUREID TOP LEVEL
---------------- ---------
1001 FLEET
1002 FLEET
1004 FLEET
1022 ACTIVETRANSPORTATION
1023 ACTIVETRANSPORTATION
1024 ACTIVETRANSPORTATION
1085 ACTIVETRANSPORTATION
1091 ACTIVETRANSPORTATION
是否可以使用 Oracle 18c SQL 执行此操作?
您可以使用 hierarchical query, with connect_by_root
:
select classstructureid, connect_by_root(classificationid)
from classstructure
connect by parent = prior classstructureid
start with parent is null;