2

我有 3 张桌子。

  1. 座位表
  2. 当地雇员
  3. 外籍员工。

在 Seat 和 Local 之间有一个共同的列 person_id,它告诉哪个人坐在哪个座位上。

在 Seat 和 Foreign 表之间以同样的方式,还有 person_id。

我能够在 Seat 和 Local ... 以及 Seat 和 Foreign 之间获取所需的数据,使用左外连接获取空座位的空座位和占用座位的 personid。

我的问题是,我可以获得一个包含两个表的查询映射座位表并获得一个综合报告吗?

现在使用的查询是:

select seat.apeh05_person_id_k
      ,seat.apeh18_seat_r seatNo
      , seat.apeh17_floor_k  seatFloor
      ,vendor.apeh15_cds_d cdsid
      , vendor.apeh15_first_n firstname
      , vendor.apeh15_last_n lastname
      ,vendor.apeh15_supervisor_cds_d ll6cdsid
      ,vendor.apeh15_ll5_cds_d ll5cdsid
      , vendor.apeh15_ll4_cds_d ll4cdsid 
 from iapeh18_seat seat ,
       IAPEH15_VENDOR_EMPLOYEE  vendor
 where seat.apeh05_person_id_k = vendor.apeh15_vendor_employee_k (+) 
 order by   seat.apeh05_person_id_k asc

另一个查询是:

select seat.apeh05_person_id_k
      ,seat.apeh18_seat_r seatNo
      , seat.apeh17_floor_k  seatFloor
      ,local.apeh09_cds_d cdsid
      ,local.apeh09_first_n firstname
      , local.apeh09_last_n lastname
      ,local.apeh09_supervisor_cds_d ll6cdsid
      ,local.apeh09_ll5_cds_d ll5cdsid
      ,  local.apeh09_ll4_cds_d ll4cdsid 
 from iapeh18_seat seat 
        , IAPEH09_LOCAL_EMPLOYEE local
 where seat.apeh05_person_id_k = local.apeh05_candidate_k (+)
 order by seat.apeh05_person_id_k asc
4

3 回答 3

3

可能是 Remko Jansen 答案的更有效版本

select seat.apeh05_person_id_k person_id
      ,seat.apeh18_seat_r seatNo
      ,seat.apeh17_floor_k  seatFloor
      ,employee.apeh15_cds_d cdsid
      ,employee.apeh15_first_n firstname
      ,employee.apeh15_last_n lastname
      ,employee.apeh15_supervisor_cds_d ll6cdsid
      ,employee.apeh15_ll5_cds_d ll5cdsid
      ,employee.apeh15_ll4_cds_d ll4cdsid 
  from iapeh18_seat seat ,
    (select * from IAPEH15_VENDOR_EMPLOYEE  
      union all
     select * from IAPEH09_LOCAL_EMPLOYEE
    ) employee
   where seat.apeh05_person_id_k = employee.apeh05_candidate_k (+)
   order by apeh05_person_id_k

起初联合并在那之后加入 - 省去执行不同行的麻烦(相当昂贵的操作)。

于 2012-12-19T12:36:18.650 回答
1

你可以,像这样,但我不确定这是否是你想要的。如果本地和供应商在 apeh05_person_id_k 中都有一个 id 的人,你想发生什么?

此外,您通常需要外键约束,但现在这是不可能的,因为 seat.apeh05_person_id_k 可以包含两个表中的 id。

总而言之,我认为这是您要求的,但我认为您的设计存在缺陷。

select 
  seat.*, -- Omitted field list for readability
  nvl2(local.apeh05_candidate_k, local.WhateverField, vendor.WhateverField) as WhateverField,
  nvl2(local.apeh05_candidate_k, local.YetAnotherField, vendor.YetAnotherField) as YetAnotherField
from 
  iapeh18_seat seat
  LEFT JOIN IAPEH15_VENDOR_EMPLOYEE vendor
    ON seat.apeh05_person_id_k = vendor.apeh15_vendor_employee_k
  LEFT JOIN IAPEH09_LOCAL_EMPLOYEE local
    ON seat.apeh05_person_id_k = local.apeh05_candidate_k
order by   
  seat.apeh05_person_id_k asc
于 2012-12-19T12:08:06.540 回答
1

由于两个查询都包含完全相同的列,您可以使用 UNION 语句将它们合并在一起,如下所示:

select seat.apeh05_person_id_k person_id
      ,seat.apeh18_seat_r seatNo
      ,seat.apeh17_floor_k  seatFloor
      ,vendor.apeh15_cds_d cdsid
      ,vendor.apeh15_first_n firstname
      ,vendor.apeh15_last_n lastname
      ,vendor.apeh15_supervisor_cds_d ll6cdsid
      ,vendor.apeh15_ll5_cds_d ll5cdsid
      ,vendor.apeh15_ll4_cds_d ll4cdsid 
  from iapeh18_seat seat ,
       IAPEH15_VENDOR_EMPLOYEE  vendor
 where seat.apeh05_person_id_k = vendor.apeh15_vendor_employee_k (+) 
UNION
select seat.apeh05_person_id_k person_id
      ,seat.apeh18_seat_r seatNo
      ,seat.apeh17_floor_k  seatFloor
      ,local.apeh09_cds_d cdsid
      ,local.apeh09_first_n firstname
      ,local.apeh09_last_n lastname
      ,local.apeh09_supervisor_cds_d ll6cdsid
      ,local.apeh09_ll5_cds_d ll5cdsid
      , local.apeh09_ll4_cds_d ll4cdsid 
 from iapeh18_seat seat 
        , IAPEH09_LOCAL_EMPLOYEE local
 where seat.apeh05_person_id_k = local.apeh05_candidate_k (+)
 order by person_id

UNION 运算符返回出现在任一结果中的所有不同行。请参阅:UNION [ALL]、INTERSECT、MINUS 运算符

于 2012-12-19T12:17:54.917 回答