为什么我不能进行完全外连接

时间:2015-07-21 15:15:00

标签: sql db2

我有一个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


当我尝试进行完全外部联接时,有人可以告诉我,它说“此查询不支持完全外部加入”#39 ;?我将如何克服这一点?

我认为这是因为其他左连接。

1 个答案:

答案 0 :(得分: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
相关问题