I need to import data from a database. The contacts are linked to companies in two ways : via contact informations, or via an “activity”. I need to get some lists.
The list of contacts without companies is easy enough :
where idcontact not in (select idcontact from tb_contact_soc where idcontact is not null)
and idcontact not in (select idcontact from tb_activite_soc_contact where idcontact is not null)
The list of contacts with multiple companies is more tricky.
idcontact is the id of the contact ; ident is the id of the company.
Tb_contact_soc is the table that links via contact infos [idcontact, ident, telephone, fax] ; tb_activite_soc_contact is the table that links via activities [idcontact,ident,activityCode].
where
(
-- contacts linked via their contact informations
idcontact in (
select idcontact
from tb_contact_soc
where idcontact is not null and ident is not null
group by idcontact
having count(*) > 1
)
-- contacts linked via an activity
or idcontact in (
select idcontact
from tb_activite_soc_contact
where idcontact is not null and ident is not null
group by idcontact
having count(*) > 1
)
) and (
-- here goes the snipplet I can't figure out
)
In the last “and”, I need to say something like “where, for this contact, at least one of the companies from the tb_contact_soc table is not found in the companies from the tb_activite_soc_contact table”. But I can’t figure it out.
I only need to get it once, so our customer can validate the data before we launch the import, so it can be a bit convulted (not too much, thanks 😉 )
Try this and clause(I assume you are using SQL 2005 or higher):