Solved

How do you get the records that don't match between 2 tables in SQL Server 2005?

Posted on 2013-11-10
7
486 Views
Last Modified: 2013-11-10
I am developing an Access application using SQL Server 2005

The following gives me the records that match between the 2 tables:

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
INNER JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber

How do you get the records that don't match between the 2 tables?
0
Comment
Question by:zimmer9
  • 4
  • 2
7 Comments
 
LVL 21
ID: 39637414
TIP: Access has an unmatched query wizard that will help you create the query.

Try something like this:

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
LEFT JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber 
WHERE
((P.CustomerNumber is Null) and (P.OfficeNumber is Null))

Open in new window

0
 

Author Comment

by:zimmer9
ID: 39637431
I'm using Access 2003 with Access as the front end and SQL Server 2005 as the back end database. I don't see the unmatched query wizard under these conditions. I am using an ADP type file extension.

I should have noted in my question that there is a 1 to many relationship between the dbo.tblCustomersNew table and the dbo.tblProductsNew table.
0
 

Author Comment

by:zimmer9
ID: 39637447
SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
LEFT JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber
WHERE
((P.CustomerNumber is Null) and (P.OfficeNumber is Null))
---------------------------------------------------------------------------------

I executed this and it returned 0 records.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zimmer9
ID: 39637452
select count(*) from dbo.tblProductsNew returns  208565

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
INNER JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber             returns 207206 records
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39637464
this will give you entries from each table that do not match... maybe another expert can combine them into one query

SELECT CustomerNumber  FROM dbo.tblCustomersNew WHERE CustomerNumber  not in (SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
INNER JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber )

SELECT
CustomerNumber FROM dbo.tblProductsNew WHERE CustomerNumber  not in (SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
INNER JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber )
0
 

Author Comment

by:zimmer9
ID: 39637465
SELECT
CustomerNumber FROM dbo.tblProductsNew WHERE CustomerNumber  not in (SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
INNER JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber )

results set is 208565 which is the number of records in tblProductsNew
0
 
LVL 9

Accepted Solution

by:
QuinnDex earned 500 total points
ID: 39637471
try this, i have removed your alias on CustomerNumber

SELECT
CustomerNumber FROM dbo.tblProductsNew WHERE CustomerNumber  not in (SELECT C.CustomerNumber
FROM dbo.tblCustomersNew AS C
INNER JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber )

SELECT CustomerNumber  FROM dbo.tblCustomersNew WHERE CustomerNumber  not in (SELECT  C.CustomerNumber
FROM dbo.tblCustomersNew AS C
INNER JOIN dbo.tblProductsNew AS P
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber = P.OfficeNumber )
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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