我有两张桌子:
表一
NumLig - CodPro - CodDer
901 - BSFX30 - 0140I18
898 - RSFX30 - 18
6 - MFLX - U
表二
NumLig - CodCmp - DerCmp - QtdUti - PerCmp
6 - MDFIB0008 - null - 1 - 2
6 - MDQDN0009 - null - 0,24 - 1
898 - MFLX - U - 0,942 - 2
898 - MDCAM0002 - 0,05 - 0
901 - RSFX - 18 - 1 - 2,5
901 - EDEAD0005 - 0,245 - 0
我需要的是搜索BSFX30 - 0140I18的输出为:
MDFIB0008 - null - 1 - 2
MDQDN0009 - null - 0,24 - 1
MFLX - U - 0,942 - 2
MDCAM0002 - 0,05 - 0
RSFX - 18 - 1 - 2,5
EDEAD0005 - 0,245 - 0
如果我这样做:
SELECT E622SIM.CodPro, E622SIM.CodDer, E622DER.TipCpc, E622SIM.NumLig, E622DER.CodCmp, E622DER.DerCmp, E622DER.QtdUti, E622DER.PerCmp
FROM E622SIM
JOIN E622DER
ON E622SIM.NumLig = E622DER.NumLig
输出是所有数据,但我需要通过CodPro和CodDer过滤。如果我这样做:
SELECT E622SIM.CodPro, E622SIM.CodDer, E622DER.TipCpc, E622SIM.NumLig, E622DER.CodCmp, E622DER.DerCmp, E622DER.QtdUti, E622DER.PerCmp
FROM E622SIM
JOIN E622DER
ON E622SIM.NumLig = E622DER.NumLig
输出仅为:
RSFX - 18 - 1 - 2,5
EDEAD0005 - 0,245 - 0
但我需要:
MDFIB0008 - null - 1 - 2
MDQDN0009 - null - 0,24 - 1
MFLX - U - 0,942 - 2
MDCAM0002 - 0,05 - 0
RSFX - 18 - 1 - 2,5
EDEAD0005 - 0,245 - 0
这是因为RSFX是BSFX30的孩子,而MFLX是RSFX的孩子
谢谢!
答案 0 :(得分:0)
我认为您需要查看有关Hierarchical Queries的文档
要使用connect by
连接您的数据,以获取根元素 - connect_by_root
,要按root过滤元素,您需要使用where connect_by_root(E622SIM.CodPro) = 'BSFX30'
我尝试将您的描述编译成脚本
create table E622SIM(NumLig int, CodPro varchar2(100), CodDer varchar2(100));
insert into E622SIM values (901, 'BSFX30', '0140I18');
insert into E622SIM values (898, 'RSFX30', '18');
insert into E622SIM values (6, 'MFLX', 'U');
create table E622DER (NumLig int, CodCmp varchar2(100), DerCmp varchar2(100), QtdUti varchar2(100), PerCmp varchar2(100));
insert into E622DER values(901, 'RSFX30',18,1,'2,5');
insert into E622DER values(901, 'EDEAD0005',0,245,0);
insert into E622DER values(898, 'MFLX','U','0,942',2);
insert into E622DER values(898, 'MDCAM0002',null,'0,05',0);
insert into E622DER values(6, 'MDFIB0008', null, 1, 2);
insert into E622DER values(6, 'MDQDN0009',null,'0,24',1);
这是一个看起来像你需要的查询
select E622DER.CodCmp
,E622DER.DerCmp
,E622DER.QtdUti
,E622DER.PerCmp
from E622DER
left outer join E622SIM on E622SIM.NumLig = E622DER.NumLig
where connect_by_root(E622SIM.CodPro) in ('BSFX30')
connect by prior E622DER.CodCmp = E622SIM.CodPro
order by level desc, E622DER.CodCmp asc
CODCMP DERCMP QTDUTI PERCMP
1 MDFIB0008 1 2
2 MDQDN0009 0,24 1
3 MDCAM0002 0,05 0
4 MFLX U 0,942 2
5 EDEAD0005 0 245 0
6 RSFX30 18 1 2,5