2012-12-19 75 views
2

我有3個表格。3個表格之間的左外連接

  1. 座位表
  2. 本地員工
  3. 外籍員工。

Seat和Local之間有一個共同的列,person_id告訴哪個人坐在哪個座位上。

以座位和外表之間的相同方式,也是person_id。

我能夠獲得席位和當地...以及座位和外部之間的所需數據,使用左外部聯接可以爲佔用席位空置和人員空位獲取空位。

我的問題是,我可以得到一個單一的查詢映射座位表與兩個表,並得到一個綜合報告?

查詢現在使用的是:

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 

回答

3

大概的Remko揚森的回答

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 

更有效的版本,起初unioning,之後加入 - 保存做不同行的麻煩(相當昂貴的操作)。

+0

感謝knagaev..am接受你的答案,因爲它從座位表中刪除了3張桌子之間產生的副本。謝謝Remko也帶我們去了正確的道路.. – ashwinsakthi

+0

當然,在我的示例中,您需要「員工」謂詞而不是「供應商」 - 我粗心的錯誤複製粘貼:) – knagaev

+0

@knagaev修復了「僱員」前綴。 –

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 
+0

嗨Goleztrol,本地和供應商員工表不能有相同的person_id.I需要來自座席表的所有數據無論personn_id是否爲空或不爲空。如果不爲空,我需要從它存在的相應表中獲取數據。 – ashwinsakthi

+0

一個更多信息..我需要來自本地和供應商表的相同列數據...不需要重複 – ashwinsakthi

+0

稍作修改:使用nvl2檢查是否在第一個表中找到記錄。如果是這樣,請從第一個表中使用FieldX,否則從第二個表中使用它。我修改了代碼。 – GolezTrol

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運算符將返回顯示任何結果都不同行。 請參閱:The UNION [ALL], INTERSECT, MINUS Operators