Если я правильно понимаю, вам нужны прямые совпадения. Затем, когда вещи не совпадают, вы хотите «выровнять» оставшиеся строки. Я бы предложил использовать union all
для этой цели:
select a.plant, a.partnumber, a.supplier, b.supplier
from a join
b
on a.plant = b.plant and
a.partnumber = b.partnumber and
a.supplier = b.supplier
union all
select plant, partnumber, max(supplier), max(supplier_b)
from ((select a.plant, a.partnumber, a.supplier, null as supplier_b
row_number() over (partition by plant, partnumber order by supplier) as seqnum
from a
where not exists (select 1
from b
where a.plant = b.plant and
a.partnumber = b.partnumber and
a.supplier = b.supplier
)
) union all
(select b.plant, b.partnumber, null, b.supplier,
row_number() over (partition by plant, partnumber order by supplier) as seqnum
from b
where not exists (select 1
from a
where a.plant = b.plant and
a.partnumber = b.partnumber and
a.supplier = b.supplier
)
)
) ab
group by plant, partnumber, seqnum;