Solved

MS SQL Left Outer JOIN with CASE Statement for Missing Records

Posted on 2013-11-14
3
2,109 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
[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
3 Comments
 
LVL 25

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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