Bagaimana kalau menggunakan join
? Berikut ini menunjukkan semua pasangan yang berbeda:
select tb.*, ts.*
from company tb join
company ts
on tb.company_name = ts.company_name and
ts.address_type = 'shipping' and
tb.address_type = 'billing' and
ts.address <> tb.address;
Jika Anda hanya menginginkan perusahaan yang berbeda:
select company_name
from company t
group by company_name
having count(distinct case when t.address_type = 'billing' then address end) = 1 and
count(distinct case when t.address_type = 'shipping' then address end) = 1 and
(max(case when t.address_type = 'billing' then address end) <>
max(case when t.address_type = 'shipping' then address end)
);
Catatan:ini juga memeriksa bahwa hanya ada satu alamat penagihan dan pengiriman yang berbeda.