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
225 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
  • 3
  • 2
5 Comments
 
LVL 142

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 142

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 142

Expert Comment

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

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now