通过子查询与组进行外连接

时间:2011-12-16 13:08:09

标签: sql database oracle outer-join

我尝试在Oracle 10g数据库中执行查询以加入两个子查询,如下所示:

SELECT * FROM(
select count(y), x from a group by y) t1 
full join 
(select count(z), x from b group by z) t2 
on (t1.x = t2.x)

问题是输出显示x的两个不同列,如下所示:

  y           z           x           x1        
-------------------------------------------
  2           4           1           1           
  3           (null)      2           (null)      
  2           (null)      3           (null)      
  8           (null)      4           (null)      
  (null)      4           (null)      5           
  (null)      6           (null)      6

有人可以帮助我吗?提前致谢!

1 个答案:

答案 0 :(得分:1)

我怀疑你想要的是:

SELECT coalesce(t1.x, t2.x) x, t1.y, t2.z
FROM (select count(y), x from a group by x) t1 
full join (select count(z), x from b group by x) t2 on (t1.x = t2.x)