Solved

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

Posted on 2013-11-10
1
357 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

22 Experts available now in Live!

Get 1:1 Help Now