Hvad med at bruge en join
? Det følgende viser alle par, der er forskellige:
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;
Hvis du bare vil have de virksomheder, der er anderledes:
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)
);
Bemærk:dette kontrollerer også, at der kun er én særskilt fakturerings- og leveringsadresse.