2

我有一张classstructure桌子:

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

我想将记录折叠到层次结构路径:

HIERARCHYPATH
---------------------------
FLEET \ LIGHTDUTYVEHICLE
FLEET \ MEDIUMDUTYVEHICLE 

ACTIVETRANSPORTATION \ FACILITYWALKWAY
ACTIVETRANSPORTATION \ TRAIL
ACTIVETRANSPORTATION \ SIDEWALK
ACTIVETRANSPORTATION \ SIDEWALK \ SDWRAMP

我怎样才能做到这一点?

4

3 回答 3

2

您可以使用sys_connect_by_path()(因为Oracle 10g Release 2)功能:

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
于 2019-09-21T13:07:40.313 回答
0

编辑1:

写完这个问题后,我意识到我应该包含层次结构的第一级行。

所以这是@Barbaros 查询的更新版本。我删除了 WHERE 子句。

select ltrim(sys_connect_by_path(classificationid, ' \ '),' \ ') as hierarchypath
from maximo.classstructure
start with parent is null
connect by prior classstructureid = parent;

编辑2:

对于奖励积分,这里的查询版本还包括 Maximo 的 USE WITH 对象(按 & 连接分组):

select 
    ltrim(sys_connect_by_path(cl.classificationid, ' \ '),' \ ') as hierarchypath,
    cl.classstructureid,
    cl.description,
    uw.usewith
from 
    maximo.classstructure cl
left join
    (
    select 
        classstructureid, 
        listagg(objectname,', ') within group(order by objectname) as usewith
    from 
        maximo.classusewith
    group by 
        classstructureid
    ) uw
    on cl.classstructureid = uw.classstructureid
start with parent is null
connect by prior cl.classstructureid = parent
order by
    hierarchypath

在此处输入图像描述


编辑3:

附带说明一下,这里是如何在子查询中对USEWITH 进行分组(对于我们不能使用 JOIN 子句的场景,只能使用 WHERE 子句)。

当然,子查询的性能比正确的连接差很多。我们希望尽可能使用连接而不是子查询。

select 
    cl.classstructureid,
    cl.description,
   
    (select 
        listagg(objectname,', ') within group(order by objectname) as usewith
    from 
        maximo.classusewith
    group by 
        classstructureid
    having 
        classstructureid = cl.classstructureid
    ) as usewith

from 
    maximo.classstructure cl
order by
    description

在此处输入图像描述

于 2021-06-09T12:14:17.380 回答
0

相同类型的查询也可以应用于 Maximo资产层次结构(不仅仅是分类):

select 
    assetnum,
    ltrim(sys_connect_by_path(assetnum, ' \ '),' \ ') as path,
    level
from 
    maximo.asset
start with parent is null
connect by prior assetnum = parent

在此处输入图像描述

我还添加了一个 LEVEL 列,它为我们提供了级别编号

于 2021-10-13T23:20:02.957 回答