开发者

aggregations in case statement

I am trying to do aggregations in ca开发者_如何学Cse statement. I found 2 ways to do it. Can anyone say what the difference between the 2 is?

  1. (CASE WHEN Event = 5 THEN count(*) ELSE 0 END ) Follow_Count GROUP BY Event;

  2. SUM(CASE Event WHEN 5 THEN 1 ELSE 0 END) AS Follow_Count


Your case 1 will produce a row for each event in the table (from your group by). Your case 2 will just return 1 row.

Is there a reason that you wouldn't just write:

select count(*)
from my_table
where event = 5;


Better would be:

count(CASE Event WHEN 5 THEN 1 END) AS Follow_Count

Because 1) for count used own standart counter, 2) "else" not need (count don't count nulls)

Regards, Sayan M.


There is no significant difference. You can decide for you which is better by comparing their execution plans.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜