Oracle内部和外部联接有效组合

时间:2015-12-16 04:08:16

标签: sql oracle join

在同一个查询中组合Oracle内部和外部联接是否有效la:

select b.col1, c.col2, sum(d.col1), sum(e.col1) from 
a  
inner join b on a.xxx = b.xxx 
inner join c on a.yyy = c.yyy 
left join d on b.aaa = d.aaa and c.bbb = d.bbb
left join e on b.aaa = e.bbb and c.aaa = e.bbb
group by b.col1, c.col2

3 个答案:

答案 0 :(得分:0)

是的,您可以按照您认为适合查询的方式组合内部,左侧和右侧连接。只需确保您了解内部,左侧和外部联接的含义。几个博客很好地描述了联接:http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

答案 1 :(得分:0)

在sum(d.col1)和sum(e.col1)值中忽略外连接空值。

Why SUM(null) is not 0 in Oracle?

如果您的null外部联接结果需要备用默认值,请考虑NVL等。

Sum columns with null values in oracle

答案 2 :(得分:0)

请尝试此操作,所有非聚合列都应该在group by子句中。

 select b.col1, c.col2, sum(d.col1), sum(e.col1) from 
    a  
    inner join b on a.xxx = b.xxx 
    inner join c on a.yyy = c.yyy 
    left join d on b.aaa = d.aaa and c.bbb = d.bbb
    left join e on b.aaa = e.bbb and c.aaa = e.bbb
    group by b.col1, c.col2