May 9, 2014 at 11:24 am
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.
May 9, 2014 at 11:42 am
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
May 9, 2014 at 12:01 pm
mickyT (5/9/2014)
HiI 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
May 9, 2014 at 12:24 pm
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
May 9, 2014 at 12:35 pm
Sorry ... of course we should ... I'll fix it up.
May 9, 2014 at 12:42 pm
No Worries.
I believe I forgot the second END which was causing the error.
Its Working now.. Thanks a lot!
May 9, 2014 at 12:51 pm
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
May 9, 2014 at 12:55 pm
Luis C., when I looked at the code I thought I was missing something about Avg, now I know.
May 9, 2014 at 1:01 pm
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:-)
May 9, 2014 at 1:58 pm
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