Solved

MS SQL Left Outer JOIN with CASE Statement for Missing Records

Posted on 2013-11-14
3
2,010 Views
Last Modified: 2013-11-14
I would like to execute a single Select statement that returns correct results for missing records of a Left Outer Join.

LeftTable.ID   RightTable.ID
1                       1
2                       2
3                      
4                       4

Select
     ISNULL(R.ID,'MISSING') as 'Record Status',
     L.ID as 'Left ID',
     R.ID as 'Right ID',
     CASE
          WHEN R.ID ISNULL THEN 'Missing'
     ELSE
          'Available'
     End as 'Case Result'
From LeftTable L
     LEFT OUTER JOIN RightTable R ON L.ID = R.ID

The Case Statement does not function correctly, because R.ID does not exist for every L.ID.  While the Results Pane will show R.ID = 'NULL', it is Missing rather than NULL. so 'Case Result' = 'Available' when it really is not available.

Is there a method to use inside a Case Statement to evaluate when a record is missing from a joined table?
0
Comment
Question by:MegaSource
3 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39649676
I think there is a typo in your statement. You have typed ISNULL, but I think IS NULL was what you meant to type:
Select
     L.ID as 'Left ID',
     R.ID as 'Right ID',
     CASE
          WHEN R.ID IS NULL THEN 'Missing'
     ELSE
          'Available'
     End as 'Case Result'
From LeftTable L
     LEFT OUTER JOIN RightTable R ON L.ID = R.ID

Open in new window

0
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 39649685
I'm not sure I understand your question.

When doing an LEFT OUTER JOIN on a table, missing rows are returned as NULL.

That is the mechanism to determine that a matching row is missing - the fact that it is returned as a NULL.

The 4th column in your query is correctly responding to the CASE statement, returning 'Available' and 'Missing'

Doesn't the following slight modification give you what you're wanting to see ?

Select 
     ISNULL(R.ID,'MISSING') as 'Record Status',
     L.ID as 'Left ID',
     CASE when R.ID IS NOT NULL 
       THEN R.ID 
       ELSE 'Missing' 
     END as 'Right ID'
From (select '1' as ID union select '2' union select '3' union select '4') as L
LEFT OUTER JOIN (select '1' as ID union select '2' union select '4')as R 
  ON L.ID = R.ID

Open in new window

0
 

Author Closing Comment

by:MegaSource
ID: 39649723
I was being dull headed.  All I had to do, based upon your help, was turn the Case When clause around and test for Not Null rather than Null.

Thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

21 Experts available now in Live!

Get 1:1 Help Now