ORACLE SQL查询在6个表

时间:2018-01-31 15:45:13

标签: sql oracle oracle11g inner-join

我正在使用ORACLE 11G。还使用Oracle应用程序快递4.2。我有一个返回重复的SQL查询。我正在使用内部联接来加入多个表。我总共加入6桌。我在下面提供了我的表架构的屏幕截图。我下面也有样本重复数据。可能是什么原因导致我获得数百行重复数据?任何帮助都是预先确定的。

以下是我的查询:

SELECT f.flight_nmbr, dp.part_id, dp.mfg_cage_code, pif.flight_phase_name, dp.part_name
   FROM flights f
       inner join flight_phases fp on f.flight_nmbr = fp.flight_nmbr
       inner join part_instance_flight_phases pif on fp.flight_nmbr = pif.flight_nmbr
       inner join part_instances pin on pif.part_instance_nmbr = pin.part_instance_nmbr
       inner join hardware_parts hp on pin.part_id = hp.part_id
       inner join designed_parts dp on hp.part_id = dp.part_id
       order by dp.part_id asc;

以下是添加了ORACLE APEX的相同查询:

SELECT f.flight_nmbr, dp.part_id, dp.mfg_cage_code, pif.flight_phase_name, dp.part_name
   FROM flights f
     inner join flight_phases fp on f.flight_nmbr = fp.flight_nmbr
     inner join part_instance_flight_phases pif on fp.flight_nmbr = pif.flight_nmbr
     inner join part_instances pin on pif.part_instance_nmbr = pin.part_instance_nmbr
     inner join hardware_parts hp on pin.part_id = hp.part_id
     inner join designed_parts dp on hp.part_id = dp.part_id
   WHERE ( f.flight_nmbr = :P100_FLIGHT_LOV1) 
     OR ( f.flight_nmbr = :P100_FLIGHT_LOV2) 
     OR ( f.flight_nmbr = :P100_X) 
     OR ( f.flight_nmbr = :P100_X2)

以下是重复数据:

Part_ID          Cage   Part_name   Flight_phase
-------------- ------   ---------   ------------
52S SE592F-52S   21356  SOYUZ MS    LAUNCH 
52S SE592F-52S   21356  SOYUZ MS    RETURN 
52S SE592F-52S   21356  SOYUZ MS    LAUNCH 
52S SE592F-52S   NAMWG  SOYUZ       LAUNCH 
52S SE592F-52S   NAMWG  SOYUZ       RETURN 
52S SE592F-52S   NAMWG  SOYUZ       LAUNCH 
52S SE592F-52S   21356  SOYUZ MS    LAUNCH 
52S SE592F-52S   21356  SOYUZ MS    RETURN 
52S SE592F-52S   21356  SOYUZ MS    LAUNCH 

这是我的数据应显示的正确输出:

Part_ID             Cage    Part_name           Flight_phase
--------------      ------ ---------            ------------
СПМ-9061-60         RSA00  SLEEPING BAG         LAUNCH 
SE592F-52S          21356  SOYUZ MS             LAUNCH
ATHSOYUZTMA         NAMWG  SOYUZ TMA VEHICLE    ORBIT
SE592F              NAMWG  SOYUZ VEHICLE        ORBIT

以下是架构的屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:2)

所有主键都是复合键,包含多个列。但是你的所有联接都在一个列上。这意味着您的结果集是一个产品,每个列实例组合都有一行。

例如,flightsflight_phases之间的联接应加入flight_nmbr, data_version_nmbr。所以因为你只加入flight_nmbr,你会得到重复的#34;每个表中data_version_nmbrflight_nmbr组合的记录。

解决方案很明显:重新编辑连接以包含外键引用中的所有列。

答案 1 :(得分:0)

这个查询对我有用。我像APC一样修好了我的联盟,我能够得到我想要的结果。 这是我的最终查询:

SELECT DISTINCT
   FIRST_VALUE(dp.part_name)
      OVER(PARTITION BY dp.part_id) as Part_Name,
      FIRST_VALUE(dp.mfg_cage_code) OVER (PARTITION BY dp.part_id) as CAGE
  ,dp.part_id as Part_Number
FROM designed_parts dp
    inner join hardware_parts hp on dp.part_id = hp.part_id
        AND dp.mfg_cage_code = hp.mfg_cage_code
        AND dp.data_version_name = hp.data_version_name
    inner join part_instances pin on hp.part_id = pin.part_id 
        AND hp.mfg_cage_code = pin.mfg_cage_code
        AND hp.data_version_name = pin.data_version_name
    inner join part_instance_flight_phases pif on pin.part_instance_nmbr = pif.part_instance_nmbr
        AND pin.part_id = pif.part_id
        AND pin.mfg_cage_code = pif.mfg_cage_code
        AND pin.data_version_name = pif.data_version_name
    inner join flight_phases fp on pif.flight_nmbr = fp.flight_nmbr
        AND pif.flight_phase_name = fp.flight_phase_name
        AND pif.data_version_name = fp.data_version_name
    inner join flights f on fp.flight_nmbr = f.flight_nmbr 
        AND fp.data_version_name = f.data_version_name
WHERE dp.data_version_name = 'WORKING'
    AND ( f.flight_nmbr = :P100_FLIGHT_LOV1 
            OR  f.flight_nmbr = :P100_FLIGHT_LOV2 
            OR  f.flight_nmbr = :P100_X 
            OR  f.flight_nmbr = :P100_X2)
ORDER BY dp.part_id ASC