Link to home
Start Free TrialLog in
Avatar of gfranco
gfrancoFlag for United States of America

asked on

Extract unmatched records from Access

Hi,

I have two tables with this structure

tblSOM
=======
ID         Contract         Cliente          Region
1           765                 A                 R1
1           1567               D                 R1
2           766                 B                 R2
3           777                 C                 R3
4           888                 D                 R4
5          9999                E                  R5

tblContracts
========

ID         Contract         Cliente          Region
1           765                 A                 R1
1           1567               D                 R1
1           99990             Z                 R2
2           11                   A                 R3
2           766                 B                 R2
3           777                 C                 R3
3          23234              18                R2
4           888                  D                 R4
5          999901             E                  R5

So, I would like to get the all contracts that not exits on the table tblContracts
The match criteria is the ID
For example on this 2 tables my final result would be the list of the contract that not in on table tblSOM

qryFinal
ID         Contract         Cliente          Region
1           99990               Z                 R2
2           11                     A                 R3
3          23234               18                R2
4           888                   D                 R4
5          999901              E                  R5

Avatar of ralmada
ralmada
Flag of Canada image

something like this?

select * from tblContract
left join tblSOM on tblContract.ID = tblSOM.ID
where tblContract.ID is null
Sorry,
select * from tblContract
left join tblSOM on tblContract.ID = tblSOM.ID
where tblSOM.ID is null
Avatar of Sharath S
you can try this.
SELECT *
  FROM tblSOM 
 WHERE tblSOM.[Contract] NOT EXISTS (SELECT 1 FROM tblContract WHERE tblSOM.[Contract] = tblContract.[Contract])

Open in new window

typo in last post. try this.
SELECT *
  FROM tblSOM 
 WHERE NOT EXISTS (SELECT 1 FROM tblContract WHERE tblSOM.[Contract] = tblContract.[Contract])

Open in new window

Avatar of gfranco

ASKER

Why 1 on this select? (SELECT 1 FROM tblContract WHERE tblSOM.[Contract] = tblContract.[Contract])
I don't think there is a need to create a subquery. Have you tried mine?
This query will produce what you are expecting for: "final result would be the list of the contract that not in on table tblSOM"

select * from tblContract
left join tblSOM on tblContract.ID = tblSOM.ID
where tblSOM.ID is null
Avatar of gfranco

ASKER

But you know what
It could be something like this

1 ID will be related to 1 or many contracts
But 1 contract is only be related to 1 ID.

I hope this clarify any doubts.


Both ralmada and my queries will give you the expected result. The 1 in the SELECT clause is nothing but I am ticking the matching record. By using NOT EXISTS, I am selecting unmatched (unticked) records.
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gfranco

ASKER

This is the resutl that i was looking for.

Thanks.
SELECT tblSomContracts.ContractNumber, tblSomContracts.[Service Level], tblSomContracts.[End Customer], tblSomContracts.[BID to partner], tblSomContracts.Partner, tblSomContracts.Region INTO tbl
FROM tblSomContracts LEFT JOIN StrategicAccounts ON tblSomContracts.ContractNumber = StrategicAccounts.Contract
WHERE (((tblSomContracts.[BID to partner]) In (Select distinct BillToPartner from StrategicAccounts)) AND ((StrategicAccounts.Contract) Is Null));

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial