May 15, 2013 at 7:50 am
Nice follow up Hugo. I didn't over-think it today. π
A minor typo in the explanation.
"... As soon as one evaluates to True, the corresponding ELSE is returned. ..."
ELSE should be THEN in that sentence.
Enjoy!
May 15, 2013 at 8:39 am
Good question, thanks.
Hakim Ali
www.sqlzen.com
May 15, 2013 at 8:46 am
Nice question Hugo. I use a lot of case statements so these are always fun to navigate.
May 15, 2013 at 8:52 am
In the Explanation: "A CASE expression will always evaluate the WHEN clauses from top to bottom. As soon as one evaluates to True, the corresponding [ELSE] is returned."
I think it should be the corresponding [THEN] not else, correct me if I am wrong.
And many thanks to SSC editors who put those 2 nice questions back to back to get the most benefit out of it.
Regards,
Hany Helmy
May 15, 2013 at 9:03 am
I too lokked for the catch, didn't find one so submitted my answer.
Thanks for the 2 points Hugo.
(Well, some question setters do try to catch us out don't they?)
May 15, 2013 at 9:30 am
Dave62 (5/15/2013)
A minor typo in the explanation."... As soon as one evaluates to True, the corresponding ELSE is returned. ..."
ELSE should be THEN in that sentence.
and
hany.helmy (5/15/2013)
In the Explanation: "A CASE expression will always evaluate the WHEN clauses from top to bottom. As soon as one evaluates to True, the corresponding [ELSE] is returned."I think it should be the corresponding [THEN] not else, correct me if I am wrong.
You are both absolutely right. Sorry for that mistake. Unfortunately, I am unable to change the explanation. π
May 15, 2013 at 10:05 am
It was so simple that I suspected a trick. π
Thanks, Hugo!
May 15, 2013 at 10:38 am
I suspected a question like this would be coming soon, so I practiced yesterday! π
May 15, 2013 at 11:01 am
I was also looking for the trick:)
Regarding the aggregates & case evaluation:
the CASE evaluation order is guaranteed for the SELECT phase. I think it helps to understand it when one thinks about the aggregation as a type of "subquery".
Example: The following query throws an error because the aggregate "MIN(1/0)" cannot be calculated.
DECLARE @i int = 1;
SELECT CASE WHEN @i = 1 THEN 1 ELSE MIN(1/0) END Agg
FROM sys.objects
This query is logically the same as the following query:
DECLARE @i int = 1
SELECT CASE WHEN @i = 1 THEN 1 ELSE AggSub END Agg
FROM (SELECT MIN(1/0) AggSub
FROM sys.objects
) S
I think that in this version, one can easier see when the error actually happens --> before the case evaluation starts. In the first version, the location of the aggregate function can mislead people to assume that this aggregate is only calculated during the evaluation of the CASE statement. But it is not. It is already performed before the CASE evaluation starts.
And to provide an example with aggregates that shows that the evaluation order is honored, consider this one:
DECLARE @i int = 1
SELECT CASE WHEN @i = 1 THEN 1 ELSE MIN(1) / 0 END Agg
FROM sys.objects
In this example, there is no error in the aggregation phase, as MIN(1) produces no error.
And there is no error in the SELECT phase, because the CASE evaluation stops right after the first WHEN (which is before the ELSE part which would cause an error).
And for completeness, here is the version in Subquery format:
DECLARE @i int = 1
SELECT CASE WHEN @i = 1 THEN 1 ELSE AggSub / 0 END Agg
FROM (SELECT MIN(1) AggSub
FROM sys.objects
) S
Best Regards,
Chris BΓΌttner
May 15, 2013 at 12:26 pm
Very positive knowledge acquired.
Thanks Hugo!
May 16, 2013 at 1:53 am
Good hump day QotD thanks
Hope this helps...
Ford Fairlane
Rock and Roll Detective
May 16, 2013 at 2:51 am
Thanks Hugo..learned something new...
May 16, 2013 at 2:56 am
Nice one Hugo..learned something....
May 16, 2013 at 2:27 pm
I thought about this one too long but still got there. Nice!
Not all gray hairs are Dinosaurs!
May 17, 2013 at 3:22 am
Another great question, thanks Hugo.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
Viewing 15 posts - 16 through 30 (of 33 total)
You must be logged in to reply to this topic. Login to reply