Solved

How to account for the difference between 2 tables records using SQL Server 2005?

Posted on 2013-11-10
1
355 Views
Last Modified: 2013-11-10
I am trying to reconcile the difference between 2 tables using SQL Server 2005.
The tables are titled dbo.tblCustomersNew and dbo.tblProductsNew.

There is a 1 to many relationship between dbo.tblCustomersNew and dbo.tblProductsNew

Here is what I have found out so far:

select count(*) from dbo.tblProductsNew                   result set is 208565 records
----------------------------------------------------------------------------------------------------

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  
               
---------------------------------------------result set is 207206 records Matching records            
----------------------------------------------------------------------------------------------------

208565 minus 207206 = 1359 records difference
----------------------------------------------------------------------------------------------------
SELECT CustomerNumber, OfficeNumber 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)                    result set is 782 records
0
Comment
Question by:zimmer9
1 Comment
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39637630
To find missing CustomersNew entries you need to use left join with a where clause, like this:
SELECT P.CustomerNumber, P.OfficeNumber
FROM dbo.tblProductsNew AS P
LEFT JOIN dbo.tblCustomersNew AS C
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber      = P.OfficeNumber
WHERE C.CustomerNumber IS NULL

Open in new window

To find missing ProductsNew entries use right join
SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblProductsNew AS P
RIGHT JOIN dbo.tblCustomersNew AS C
ON   C.CustomerNumber = P.CustomerNumber
AND C.OfficeNumber      = P.OfficeNumber
WHERE P.CustomerNumber IS NULL

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

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 …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

23 Experts available now in Live!

Get 1:1 Help Now