I would suggest using a FULL OUTER JOIN to join the tables on the day
column to get the result:
select coalesce(t1.day, t2.day) "day",
coalesce(t1.acount, 0) acount,
coalesce(t2.bcount, 0) bcount
from table1 t1
full outer join table2 t2
on t1.day = t2.day;
See SQL Fiddle with Demo. The COALESCE
function will return the first non-null result, so this can be used to get the day
values in the same column and then replace the nulls
in the acount
and bcount
columns.