Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-10
1
Medium Priority
?
368 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 25

Accepted Solution

by:
chaau earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

876 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