2015-07-21 55 views
0

我有一個db2查詢,我今天意識到我需要擴展那個查詢。爲什麼我不能做一個完整的外連接

我的表已經非常複雜的連接,所以我真的不想添加聯合查詢。我想做一個完整的外連接。

目前,它顯示

 
    SELECT 
     a.id 
     ,a.city 
     ,a.state 
     ,case when a.thing = b.thing then a.thing else b.thing end 
     ,sum(case when c.thing = 'thing' then 1 else 0 end) 
     ,b.id 
     ,b.name 

    FROM 
     a 
    INNER JOIN b -- I want to change this to FULL OUTER JOIN 
     ON a.id = b.id 
    LEFT JOIN c 
     ON a.id = c.id 
    LEFT JOIN (d 
     INNER JOIN e 
      ON d.id = e.id 
     ) 
    WHERE 
     --logic 
    GROUP BY 
     --for the aggregate functions 
    ORDER BY 
     --logic 


有人能告訴我,當我嘗試做一個完全外連接,它說:「完全外部聯接不支持此查詢」?我將如何克服這一點?

我認爲這是因爲其他左連接。

+0

你有5張桌子,4'JOIN'條款,但只有3'ON'條款。這是你的意思嗎? –

+0

我相信如此。我遺漏了一些c,d和e Case語句和select語句中的聚合函數。 – Emwat

+1

@Emwat。 。 。請編輯您的問題幷包含正在生成錯誤的查詢。 –

回答

0

它可能無法將外連接與左連接組合在一起。您可能需要使外連接子查詢(也加入了一些缺失的別名和ON子句):

SELECT 
    ab.a_id 
    ,ab.city 
    ,ab.state 
    ,ab.thing 
    ,sum(case when c.thing = 'thing' then 1 else 0 end) 
    ,ab.b_id 
    ,ab.name 

FROM 
(
    SELECT 
     a.id a_id 
     ,a.city 
     ,a.state 
     ,case when a.thing = b.thing then a.thing else b.thing end thing 
     ,b.id b_id 
     ,b.name 

    FROM 
     a 
    FULL OUTER JOIN b 
     ON a.id = b.id 
) ab 
LEFT JOIN c 
    ON ab.id = c.id 
LEFT JOIN (d 
    INNER JOIN e 
     ON d.id = e.id 
    ) f 
    ON ... 
WHERE 
    --logic 
GROUP BY 
    --for the aggregate functions 
ORDER BY 
    --logic 
相關問題