drop table Ա purge;
drop table  purge;

exec dbms_random.seed(150);

create table Ա
as
select rownum Ϸùȣ
     , round(dbms_random.value(1, 20)) ID
     , round(dbms_random.value(1000, 100000),-2) Աݾ
from dual connect by level <= 10;

create table 
as
select rownum Ϸùȣ
     , round(dbms_random.value(1, 20)) ID
     , round(dbms_random.value(1000, 10000),-2) ݾ
from dual connect by level <= 10;

exec dbms_stats.gather_table_stats(user, 'Ա');
exec dbms_stats.gather_table_stats(user, '');

set autotrace on exp;

select a.ID, a.Աݾ, b.ݾ
from  (select ID, sum(Աݾ) Աݾ from Ա group by ID) a
     ,(select ID, sum(ݾ) ݾ from  group by ID) b
where  b.ID(+) = a.ID
union all
select ID, null, ݾ
from  (select ID, sum(ݾ) ݾ from  group by ID) a
where not exists (select 'x' from Ա where ID = a.ID) ;


select nvl(a.ID, b.ID) ID, a.Աݾ, b.ݾ
from  (select ID, sum(Աݾ) Աݾ from Ա group by ID) a 
       full outer join
      (select ID, sum(ݾ) ݾ from  group by ID) b 
    on a.ID = b.ID ;


select /*+ opt_param('_optimizer_native_full_outer_join', 'force') */ 
       nvl(a.ID, b.ID) ID, a.Աݾ, b.ݾ
from  (select ID, sum(Աݾ) Աݾ from Ա group by ID) a 
       full outer join
      (select ID, sum(ݾ) ݾ from  group by ID) b 
    on a.ID = b.ID ;


select ID, sum(Աݾ) Աݾ, sum(ݾ) ݾ
from (
  select ID, Աݾ, to_number(null) ݾ
  from   Ա
  union all
  select ID, to_number(null) Աݾ, ݾ
  from   
)
group by ID ;

