Solved

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

Posted on 2013-11-10
7
487 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

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!

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…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

689 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