If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Using select statement inside a CASE WHEN expression

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-11-11, 07:02
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
Using select statement inside a CASE WHEN expression

Hello,
Need help in writing query for the below scenario:

I have a table T1 which has columns say TM_ID and ALT_ID. I need to select a set of TM_IDs. If this ALT_ID has a value zero the I need to select all TM_IDs from another table T2 else the TM_ID from T1

So here is the question.

I need to select TM_IDs from table T1 for a particular ALT_ID. IF TM_ID is zero then I need to select all teams from another table T2. Else TM_ID from table T1. I tried using select inside a CASE like below :

SELCT TM_ID from T1
case TM_ID
WHEN 0 THEN SELECT TM_ID from T2
else
TM_ID
END
from T1 where ALT_ID='222'

But I end up is error -104

Thanks for ur help

Thanks,
Thamin
Reply With Quote
  #2 (permalink)  
Old 07-11-11, 08:07
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Try those examples.
(both were not tested)

Example (a):
Code:
SELECT t2.*
 FROM  t2
 INNER JOIN
       t1
   ON  t1.tm_id  = t2.tm_id
   OR  t1.tm_id  = 0
 WHERE t1.alt_id = '222'
Example (b):
Code:
SELECT t2.*
 FROM  t2
 WHERE EXISTS
       (SELECT 0
         FROM  t1
         WHERE t1.alt_id = '222'
           AND
           (   t1.tm_id  = t2.tm_id
            OR t1.tm_id  = 0
           )
       )
"t1.tm_id = t2.tm_id OR t1.tm_id = 0" can be replaced by "t1.tm_id IN (t2.tm_id , 0)".

Last edited by tonkuma; 07-11-11 at 08:14. Reason: Correct spelling(SELCT to SELECT).
Reply With Quote
  #3 (permalink)  
Old 07-11-11, 09:19
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
Hello, Thanks for the reply. I tried your query and its wotking fine when there is non zero value for TM_ID in T1 and for zero value this is not fetching any rows... :-(
Reply With Quote
  #4 (permalink)  
Old 07-11-11, 09:28
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
hello,
sorry i had a misplaced the two tables...your query is working fine...but just wanted to whether we can use SELECT inside CASE WHEN ...

Thanks a lot!!!
Reply With Quote
  #5 (permalink)  
Old 07-11-11, 09:42
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
whether we can use SELECT inside CASE WHEN ...
You can't do it.

Although you can write SELECT statement(s) inside a CASE statement,
it would be not as you expected.
Reply With Quote
  #6 (permalink)  
Old 07-11-11, 10:17
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
In Example (a) and Example (b),
I assumed that all T1.tm_id were in T2.

Example (c) would be exactly what you requested.
Quote:
I need to select TM_IDs from table T1 for a particular ALT_ID.
IF TM_ID is zero then I need to select all teams from another table T2. Else TM_ID from table T1.
Example (c):
Code:
SELECT COALESCE(t2.tm_id , t1.tm_id) AS tm_id
 FROM  t1
 LEFT OUTER JOIN
       t2
   ON  t1.tm_id = 0
 WHERE t1.alt_id = '222'
;
Reply With Quote
  #7 (permalink)  
Old 07-12-11, 10:10
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
Thanks a lot Ton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On