Solved

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

Posted on 2013-11-10
1
365 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
[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
1 Comment
 
LVL 25

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

There are some very powerful Data 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 discu…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
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…

624 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