Complicated Case

  • Need help on this one...

    I need to check two scenario's

    Average Hours and Type and based on that come up with the results.

    Avg is a Int , Type is Varchar.

    Case when Avg < = 24 and Type = 'Type 1' Then Avg

    Else 24 End

    Case when Avg < 48 and (Type = 'Type2 OR Type = 'Type 3' ) Then Avg

    Else 48 End

    I am trying to get these two cases in One .

    But am stuck with the else part because I have two separate Else part for both.

  • Hi

    I guessing that the type is the main part of this, so something like

    CASE

    WHEN Type = 'Type 1' THEN

    CASE WHEN Avg <= 24 THEN Avg ELSE 24 END

    WHEN Type IN ('Type 2','Type 3') THEN

    CASE WHEN Avg < 48 THEN Avg ELSE 48 END

    ELSE -- Some other value?

    -9999

    END

    Edit: added missing whens

  • mickyT (5/9/2014)


    Hi

    I guessing that the type is the main part of this, so something like

    CASE

    WHEN Type = 'Type 1' THEN

    CASE Avg <= 24 THEN Avg ELSE 24 END

    WHEN Type IN ('Type 2','Type 3') THEN

    CASE Avg < 48 THEN Avg ELSE 48 END

    ELSE -- Some other value?

    -9999

    END

    mickyT has a good way.

    Another might be:CASE

    WHEN Type = 'Type 1' AND Avg <= 24 THEN Avg

    WHEN Avg <= 24 THEN 24

    WHEN Type IN ('Type 2','Type 3') Avg < 48 THEN Avg

    WHEN Avg < 48 THEN 48

    END

  • Thanks for the response . But

    Should we not add "When" after the 2nd and 3rd Case ?

    Also , When I do so I get an error : Incorrect syntax near the keyword 'FROM '

    There is something in the Case that needs to be Tweaked , Because when I comment the Case , it populates the remaining result

    Here it is

    Select ItemNo ,

    CASE

    WHEN Type = 'Type 1' THEN

    CASE when Avg <= 24 THEN Avg ELSE 24 END

    WHEN Type IN ('Type 2','Type 3') THEN

    CASE when Avg < 48 THEN Avg ELSE 48 END

    ELSE '48'

    END as BelowLevel

    From Prod

  • Sorry ... of course we should ... I'll fix it up.

  • No Worries.

    I believe I forgot the second END which was causing the error.

    Its Working now.. Thanks a lot!

  • From what I can understand, you're trying to limit the upper value of type 1 to 24 and types 2 and 3 to 48.

    This should do the trick.

    Case when Avg > 24 and Type = 'Type 1'

    Then 24

    when Avg > 48 and (Type = 'Type2' OR Type = 'Type 3' )

    Then 48

    Else Avg End

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis C., when I looked at the code I thought I was missing something about Avg, now I know.

  • Luis Cazares (5/9/2014)


    From what I can understand, you're trying to limit the upper value of type 1 to 24 and types 2 and 3 to 48.

    This should do the trick.

    Case when Avg > 24 and Type = 'Type 1'

    Then 24

    when Avg > 48 and (Type = 'Type2' OR Type = 'Type 3' )

    Then 48

    Else Avg End

    Nice Luis ... I thought I was over complicating it ... too early in the morning for me:-)

  • Thank you all for your help. Appreciate it.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply