Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2007
    Posts
    2

    Question Unanswered: 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.

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    where ((HAN.KOMK_1 = '1') and (UDR.TNK_MRK LIKE 'J')) or
    (HAN.KOMK_1 <> '1') and (UDR.TNK_MRK LIKE '%'))

    HTH

    Andy

  3. #3
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •