[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-11-10
7
Medium Priority
?
495 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

873 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