Solved

How would you write a query to find the records in 1 table that don't exist in another table ?

Posted on 2008-09-29
5
232 Views
Last Modified: 2013-12-05
I am developing an Access application using Access as the front end and SQL Server as the back end database. I use the following query to yield a result set of matching records.

How would you rewrite the following query to:
 
find the records in ztbl_Source_SSFIII THAT DO NOT EXIST in ztbl_Master_Template
-----------------------------------------------------------------------------------------------------
SELECT a.id, a.[Fund Name], b.[Investor Name], b.[Legal Entity]
FROM ztbl_Master_Template a
INNER JOIN ztbl_Source_SSFIII b ON b.[Investor Name] = a.[Account Name]  AND
a.[Legal Entity] LIKE '%' + b.[Legal Entity] + '%'
where [Fund Name] = 'Special Situations Fund III'
order by id
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22599744
this will do:
SELECT a.id, a.[Fund Name], b.[Investor Name], b.[Legal Entity]
FROM ztbl_Source_SSFIII b 
LEFT JOIN ztbl_Master_Template a
  ON b.[Investor Name] = a.[Account Name]  
  AND a.[Legal Entity] LIKE '%' + b.[Legal Entity] + '%' 
  AND a.[Fund Name] = 'Special Situations Fund III'
WHERE a.id IS NULL

Open in new window

0
 

Author Comment

by:zimmer9
ID: 22599924
If I perform the following query:

select count(*) from ztbl_Source_SSFIII

the result of the record count is 966 rows.
---------------------------------------------------
As a result of the following query, the result set is 724 rows:

SELECT a.id, a.[Fund Name], b.[Investor Name], b.[Legal Entity]
FROM ztbl_Master_Template a
INNER JOIN ztbl_Source_SSFIII b ON b.[Investor Name] = a.[Account Name]  AND
a.[Legal Entity] LIKE '%' + b.[Legal Entity] + '%'
where [Fund Name] = 'Special Situations Fund III'
order by id
-----------------------------------------------------------------

As a result of the following query, the result set is 243 rows:

SELECT a.id, a.[Fund Name], b.[Investor Name], b.[Legal Entity]
FROM ztbl_Source_SSFIII b
LEFT JOIN ztbl_Master_Template a
  ON b.[Investor Name] = a.[Account Name]  
  AND a.[Legal Entity] LIKE '%' + b.[Legal Entity] + '%'
  AND a.[Fund Name] = 'Special Situations Fund III'
WHERE a.id IS NULL
----------------------------------------------------------------
Thus, the record count of 966 rows is 1 less than the result of the 2 queries (724 rows + 243 rows).

Any thoughts on how I can reconcile the 1 record difference ?

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22599995
that's usually a NULL value in either legal entity or fund name?
0
 

Author Comment

by:zimmer9
ID: 22600997
Could it also be due to 2 matching records in ztbl_Master_Template for 1 record
ztbl_Source_SSFIII ?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22601048
yes, possibly.
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question