?
Solved

MS SQL Left Outer JOIN with CASE Statement for Missing Records

Posted on 2013-11-14
3
Medium Priority
?
2,182 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 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

801 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