Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

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
?
492 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Backup & Restore 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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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