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 > Conditional condition - is it possible?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-01-07, 09:59
tagawa tagawa is offline
Registered User
 
Join Date: Oct 2007
Posts: 2
Question Conditional condition - is it possible?

I'm probably overlooking something really simple (and I've tried searching first) but I can't seem to create a WHERE condition that depends on whether another condition is true or false. In other words, if a.col1 = '1' then select where b.col1 <> 0, or something to that effect. Here's what I've tried so far:

Code:
--IF HAN.KOMK_1 = '1' THEN WHERE UDR.TNK_MRK = 'J' ELSE
WHERE UDR.TNK_MRK = '%' END IF

--WHERE IF HAN.KOMK_1 = '1' THEN UDR.TNK_MRK = 'J' ELSE
UDR.TNK_MRK = '%' END IF

--WHERE UDR.TNK_MRK LIKE (IF HAN.KOMK_1 = '1' THEN 'J'
ELSE '%' END IF)

--WHERE UDR.TNK_MRK LIKE (CASE WHEN HAN.KOMK_1='1' THEN
'J' ELSE '%' END)

--WHERE UDR.TNK_MRK (CASE WHEN HAN.KOMK_1='1' THEN LIKE
'J' ELSE LIKE '%' END)

--WHERE (CASE WHEN HAN.KOMK_1='1' THEN UDR.TNK_MRK LIKE
'J' ELSE UDR.TNK_MRK LIKE '%' END)

WHERE (CASE WHEN HAN.KOMK_1='1' THEN UDR.TNK_MRK ELSE UDR.HANTEI END)
LIKE 'J'
The commented out lines are my attempts that produced errors and the final line works but does not produce the result I'm after.

I originally had this done in Java looping through each record but was told it had to be in SQL as around 6-8 millions records are being summed. Seems like a fair request but it's too much for my grey matter. Can anyone help?
Thank you in advance.
Reply With Quote
  #2 (permalink)  
Old 10-01-07, 14:01
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
where ((HAN.KOMK_1 = '1') and (UDR.TNK_MRK LIKE 'J')) or
(HAN.KOMK_1 <> '1') and (UDR.TNK_MRK LIKE '%'))

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 10-01-07, 21:42
tagawa tagawa is offline
Registered User
 
Join Date: Oct 2007
Posts: 2
Smile Proof I need a holiday

Andy, thank you so much.

I can't believe I spent so long trying to get IFs and CASEs to work when I didn't need them in the first place! My colleagues had a similar 'Doh!' moment when I showed them.

Thanks for being the fresh pair of eyes that I needed and for taking the time to post.

Tagawa
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