Solved

MS SQL Left Outer JOIN with CASE Statement for Missing Records

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MessageHandler Database in SQL way too large 4 47
MS SQL page split per second is high 19 103
Caste datetime 2 62
View SQL 2005 Job package 16 67
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

791 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