Solved

How to get a result set of non matching records using SQL Server 2005?

Posted on 2013-11-08
6
518 Views
Last Modified: 2013-11-10
I'm working with SQL Server 2005:

If the following records match:

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
LEFT JOIN dbo.tblStatesAll AS S ON S.StateFS = C.ResStateCode  
INNER JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber  
AND C.OfficeNumber = P.OfficeNumber

How would you write a SQL Statement for the records that don't match
0
Comment
Question by:zimmer9
6 Comments
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 125 total points
ID: 39634332
try this

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
LEFT JOIN dbo.tblStatesAll AS S ON S.StateFS = C.ResStateCode  
INNER JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber  
AND C.OfficeNumber = P.OfficeNumber
Where S.StateFS Is Null
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 125 total points
ID: 39634334
Try this:

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C 
LEFT JOIN dbo.tblStatesAll AS S ON S.StateFS = C.ResStateCode   
LEFT JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber  
AND C.OfficeNumber = P.OfficeNumber 
WHERE P.OfficerNumber Is NULL AND p.CustomerNumber  IS NULL

Open in new window

0
 

Author Comment

by:zimmer9
ID: 39634393
My humble apologies:

Let me restate my query:

There are actually 2 queries because of the field S.FallCycle:

I tried a record count of the table dbo.tblProductsNew
and it doesn't match the record count I get from
Query 1 + Query 2 as follows:
So I am trying to reconcile my difference.

Query 1)
SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number
FROM dbo.tblCustomersNew AS C
LEFT JOIN dbo.tblStatesAll AS S ON S.StateFS = C.ResStateCode  
INNER JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber
WHERE (S.FallCycle= 1)
-----------------------------------------------------------------------------------------------------------
Query 2)
SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM dbo.tblCustomersNew AS C
LEFT JOIN dbo.tblStatesAll AS S ON S.StateFS = C.ResStateCode   -- Use LEFT JOIN and check for NULL in StateFS for non-matching values.
INNER JOIN dbo.tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber  AND C.OfficeNumber = P.OfficeNumber
WHERE (S.FallCycle= 0)

UNION ALL

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM tblCustomersNew AS C
LEFT JOIN tblStatesAll AS S ON S.StateFS = C.ResStateCode  
INNER JOIN tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber
WHERE S.StateFS IS NULL
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:zimmer9
ID: 39634406
correction:

UNION ALL

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number]
FROM tblCustomersNew AS C
LEFT JOIN tblStatesAll AS S ON S.StateFS = C.ResStateCode  
INNER JOIN tblProductsNew AS P ON C.CustomerNumber = P.CustomerNumber AND
C.OfficeNumber = P.OfficeNumber
WHERE S.StateFS IS NULL
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39636465
>>record count ... dbo.tblProductsNew ... doesn't match ... count ... from Query 1 + Query 2
well query 1 and query 2 involve 2 other tables, and you are use new customers as the from table, so any records in new products that hasn't yet got a customer won't be included. You also have 2 conditions on one field (FallCycle) and one on another field (StateFS) so if any of those conditions aren't met then they won't be counted either.

Why not start with something like this? It assumes there is an ID field on tables
SELECT
      count(*)                                                   as count_all
    , count(distinct P.ID)                                       as p_id_distinct
    , count(distinct C.ID)                                       as c_id_distinct
    , count(case when C.ID IS NULL then P.ID end)                as c_id_null
    , count(case when S.FallCycle = 1   then P.ID end)           as FallCycle_1
    , count(case when S.FallCycle = 0   then P.ID end)           as FallCycle_0
    , count(case when S.FallCycle not in (1, 0 )  then P.ID end) as FallCycle_not
    , count(case when S.StateFS IS NULL then P.ID end)           as StateFS_null
    , count(case when S.StateFS IS NOT NULL then P.ID end)       as StateFS_not_null
FROM tblProductsNew AS P
LEFT JOIN tblCustomersNew AS C ON P.CustomerNumber = C.CustomerNumber AND P.OfficeNumber = C.OfficeNumber 
LEFT JOIN tblStatesAll    AS S ON C.ResStateCode = S.StateFS
;

Open in new window

the results of this might help identify why you can't reconcile so far.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 39636471
just an observation:
FROM tblCustomersNew AS C  --<< C is "prior to S" and "prior to P"

LEFT JOIN tblStatesAll AS S ON S.StateFS = C.ResStateCode   -- "prior table" used in join last

INNER JOIN tblProductsNew AS P
     ON C.CustomerNumber = P.CustomerNumber -- "prior table" used in join first
  AND C.OfficeNumber = P.OfficeNumber

It is my opinion that a "prior table" should always be used first in joins
however, more importantly I believe the approach should be consistently applied no matter which you list first.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

19 Experts available now in Live!

Get 1:1 Help Now