Solved

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

Posted on 2013-11-10
7
480 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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