我可以使用Oracle分层查询(CONNECT BY):
--create or replace view cgclassstructure_vw as (
select
cl.classstructureid,
level as pseudo_level, --the CLASSANCESTOR table already has an OOB HIERARCHYLEVEL column. But the level numbers in that column are in reverse-order (sorted descending) and the level numbers start at 0 instead of 1 -- which isn't what we want.
count(level) over (partition by connect_by_root(cl.classificationid)) as level_count,
cl.classificationid,
cl.description,
cl.cghierarchypath,
--ltrim(sys_connect_by_path(cl.classificationid, ' \ '),' \ ') as hierarchypath,
cl.parent,
cl.haschildren,
connect_by_root(cl.classstructureid) as top_classstructureid,
connect_by_root(cl.classificationid) as top_classificationid,
uw.usewith,
cl.cgactive
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 cl.parent is null
connect by prior cl.classstructureid = parent
order by
top_classstructureid, --handles scenarios like the FACILITIES classifications -- we have top-level FACILITIES classifications for both ASSET and WO (different sets of classifications)
cghierarchypath
--)