简体   繁体   中英

Outer join with group by subqueries

I'm trying to execute a query in an Oracle 10g DB to join two subqueries as it follows:

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)

The problem is that the output shows two different columns for x as it follows:

  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

Does anyone can help me? Thanks in advance!

I suspect that what you want is:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM