Solved

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

Posted on 2013-11-08
6
525 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
[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
6 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 49

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 49

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

627 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