Oracle中的层次SQL

Oracle中的层次SQL,sql,oracle,hierarchical,Sql,Oracle,Hierarchical,我有一个巨大的表,有很多列,所以为了简单起见,我只选择了相关的列 有组件和模块。组件可以用作最终产品,但可以在另一个组件中构建=模块。模块也是如此。一个模块可以作为最终产品,也可以构建在另一个组件中=模块 在实表中只有数字,但为了更好地理解,我使用关系组件modul 例如componentA+componentB=ModuleAB,但componentA也可以用作最终项。 因此,表中有两列,用于确定组件是否在工厂中作为最终产品使用 assembly_id-表示该项是一个模块,因此必须有一些组件c

我有一个巨大的表,有很多列,所以为了简单起见,我只选择了相关的列

有组件和模块。组件可以用作最终产品,但可以在另一个组件中构建=模块。模块也是如此。一个模块可以作为最终产品,也可以构建在另一个组件中=模块

在实表中只有数字,但为了更好地理解,我使用关系组件modul 例如componentA+componentB=ModuleAB,但componentA也可以用作最终项。 因此,表中有两列,用于确定组件是否在工厂中作为最终产品使用 assembly_id-表示该项是一个模块,因此必须有一些组件child in_assembly_id-表示此项将构建在其他项/模块中

如上所述,在这种情况下,项目可以是最终的,两列都有-1,但同时该项目可以构建在另一个itemmodul中,因此在_assembly_id-1中有另一行有列

对于已经包含某些组件的模块,可以是最终项目,也可以构建在另一个itemmodul中的项目,这一点同样适用于在_assembly_id列中指明的项目

我的目标是为特定项目找到模块项目,无论该项目是否也可以作为最终产品

我可以使用join,但只能找到一个级别

select distinct c.item, c.in_assembly_id, modul, modul_id from items c ,lateral (select a.item modul,a.assembly_id modul_id from items a where a.assembly_id=c.in_assembly_id)
where c.item = 'componentA' and c.in_assembly_id <> -1;

ITEM        IN_ASSEMBLY_ID  MODUL   MODUL_ID
componentA  100             modulAC 100
componentA  50              modulAB 50
最高模

    ITEM        IN_ASSEMBLY_ID assembly_id type/level
    componentA  -1             50          comp
    modulAB     50             -1          modul
    componentA  -1             100         comp
    modulACDE   500            -1          modul
我从connect by子句开始,但它对我不起作用,我不知道为什么我会得到modulDE

select distinct * from items
connect by nocycle in_assembly_id = prior assembly_id
start with item = 'componentA' and in_assembly_id <> '-1';
我的目标是为特定项目找到模块项目,无论该项目是否也可以作为最终产品

这将获得组件a的所有模块:

选择“按根项目id连接”作为根项目id, 按根项连接根项作为根项, 项目编号:, 项目 装配id, 系统通过路径项目id连接,作为路径项目id, 系统通过路径项连接,作为路径项 从项目 其中程序集_id-1 从 项目='componentA' 和assembly_id,in_assembly_id不在-1,-1中 通过NOCYCLE连接 Previor in_assembly_id=assembly_id 按项目、项目标识订购同级产品; 哪些产出:

产出:


dbfiddle

由于_assembly_id'-1'中的子句,无法获取任何行。你的意思是它是=?这应该会给你你想要的输出我得到的是行,但不是我在输出中预期的,with=你将从componentA开始作为最终项,不会分别得到模块Hi,我得到的同样的问题,组件D和模块DE不应该存在,组件A进入模AB和模AC,然后模AC进入另一个模ACDE,这就是我需要得到的全部。组件A不进入模块DE,因此这意味着模块DE及其组件应位于output@PatrikMelichercik您的数据模型存在的问题是,您有太多的值为-1,因此所有内容都连接到其他内容。您需要解决如何更改数据,以便连接唯一值,而不是-1。我没有创建表,也可以更改为表。因此,我认为如果我们在_assembly的列中点击值为-1的项模块,循环可以停止_id@PatrikMelichercik你能解释一下它是怎么工作的吗,我不明白一件事。如果您将assembly_id>-1放在WHERE子句中,那么componentA将在表中被过滤掉,如果它有assembly_id=-1,查询如何以componentA开始?我在我的真实表上尝试了这样的查询,我不明白这个查询可以在不停止进程的情况下找到任何东西,并且有一个组件到一个模块的简单连接。
select distinct * from items
connect by nocycle in_assembly_id = prior assembly_id
start with item = 'componentA' and in_assembly_id <> '-1';
create table items (
item_id number,
item varchar2(12),
assembly_id number,
in_assembly_id number
);

insert into items values ( 1 , 'componentA' , -1 , -1 );
insert into items values ( 2 , 'componentA' , -1 , 50 );
insert into items values ( 3 , 'componentB' , -1 , 50 );
insert into items values ( 4 , 'modulAB' , 50 , -1 );
insert into items values ( 5 , 'componentA' , -1 , 100 );
insert into items values ( 6 , 'componentC' , -1 , 100);
insert into items values ( 7 , 'modulAC' , 100 , -1 );
insert into items values ( 7 , 'modulAC' , 100 , 500 );
insert into items values ( 8 , 'componentD' , -1 , 200 );
insert into items values ( 9 , 'componentE' , -1 , 200 );
insert into items values ( 10 , 'modulDE' , 200 , 500 );
insert into items values ( 11 , 'modulACDE' , 500 , -1 );
insert into items values ( 12 , 'componentF' , -1 , -1 );
insert into items values ( 13 , 'componentG' , -1 , -1 );
ROOT_ITEM_ID | ROOT_ITEM | ITEM_ID | ITEM | ASSEMBLY_ID | PATH_ITEM_ID | PATH_ITEM -----------: | :--------- | ------: | :-------- | ----------: | :----------- | :------------------------------------- 2 | componentA | 4 | modulAB | 50 | ,2,4 | ,componentA,modulAB 2 | componentA | 7 | modulAC | 100 | ,2,4,5,7 | ,componentA,modulAB,componentA,modulAC 2 | componentA | 7 | modulAC | 100 | ,2,4,6,7 | ,componentA,modulAB,componentC,modulAC 2 | componentA | 10 | modulDE | 200 | ,2,4,8,10 | ,componentA,modulAB,componentD,modulDE 2 | componentA | 10 | modulDE | 200 | ,2,4,9,10 | ,componentA,modulAB,componentE,modulDE 5 | componentA | 7 | modulAC | 100 | ,5,7 | ,componentA,modulAC 5 | componentA | 10 | modulDE | 200 | ,5,7,8,10 | ,componentA,modulAC,componentD,modulDE 5 | componentA | 10 | modulDE | 200 | ,5,7,9,10 | ,componentA,modulAC,componentE,modulDE 5 | componentA | 7 | modulAC | 100 | ,5,7 | ,componentA,modulAC 5 | componentA | 11 | modulACDE | 500 | ,5,7,11 | ,componentA,modulAC,modulACDE ROOT_ITEM_ID | ROOT_ITEM | ITEM_ID | ITEM | ASSEMBLY_ID | PATH_ITEM_ID | PATH_ITEM -----------: | :--------- | ------: | :-------- | ----------: | :----------- | :---------------------------- 2 | componentA | 4 | modulAB | 50 | ,2,4 | ,componentA,modulAB 5 | componentA | 7 | modulAC | 100 | ,5,7 | ,componentA,modulAC 5 | componentA | 7 | modulAC | 100 | ,5,7 | ,componentA,modulAC 5 | componentA | 11 | modulACDE | 500 | ,5,7,11 | ,componentA,modulAC,modulACDE