Solved

MS SQL Left Outer JOIN with CASE Statement for Missing Records

Posted on 2013-11-14
3
2,137 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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