View Single Post
Join Date: Nov 2008
Posts: 1
Reputation: bohil is an unknown quantity at this point 
Solved Threads: 0
bohil bohil is offline Offline
Newbie Poster

how do I nest a CASE statement in an INNER JOIN

 
0
  #1
Nov 17th, 2008
Hello,

I am trying to nest some CASE statements into INNER JOINs (one CASE per INNER JOIN).

The current code is:
  1. SELECT
  2. a.zone, a.ifield, a.iwhse, a.idesc, a.iposition
  3. FROM schema.itable a
<---This is where I would like to place the INNER JOIN w/ CASE
  1. LEFT OUTER JOIN
  2. (
  3. SELECT
  4. x.icode, x.ikey, x.idesc, x.icurrent
  5. FROM schema.itablex
  6. ) b
  7. ON a.iwhse = b.icode
  8. AND a.ikey = b.ikey
  9. AND icurrent = 'true'
  10. LEFT OUTER JOIN
  11. etc. . . . .

Anyway, I really don't even know where to begin with the syntax (assuming it can be done). I've tried to do the implied INNER JOIN by leaving the INNER JOIN w/ case in this form:
  1. SELECT iitem, ikey
  2. CASE WHEN iitem IS NULL THEN 'Y'
  3. WHEN iitem = ' ' THEN 'Y'
  4. ELSE 'N"
  5. END
  6. FROM schema.itable
  7.  

And, I've also tried to spell out the INNER JOIN and put the CASE statement into the ON clause like this:
  1. INNER JOIN
  2. (
  3. SELECT iitem, ikey
  4. FROM schemai.table
  5. WHERE icurrent = 'true'
  6. )
  7. ON
  8. CASE WHEN iitem IS NULL THEN 'Y'
  9. WHEN iitem = ' ' THEN 'Y'
  10. ELSE 'N"
  11. END
  12.  
. . . also tried with parens around the CASE statement.

I'm not only new to databases and SQL, but am also new to posting on forums for assistance. So, if there's any other info that would be helpful, please let me know.

Thanks!
Last edited by bohil; Nov 17th, 2008 at 11:44 am.
Reply With Quote