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
226 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.​
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

932 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

8 Experts available now in Live!

Get 1:1 Help Now