Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Conditional aggregation - SUM Vs COUNT

Mar 26 2013 12:00AM by Madhivanan   

You can do conditional aggregation like SUM(CASE WHEN .. THEN 1 ELSE 0 END),....etc to find a count for a particular match. This type of conditions are useful when you want to write a CROSS-TAB/PIVOT Query. You can also make use of COUNT(CASE WHEN .. THEN 1 END). One of the developers told me that he used the same type of queries but the count did not seem to be correct. I asked him to show the code. His code has the following pattern

COUNT(CASE WHEN .. THEN 1 ELSE 0 END)

I immediately pointed out to him that the above condition is equivalent to COUNT(*) because COUNT will count everything other than NULL. In the above expression both 1 and 0 are counted. So either COUNT should be replaced with SUM or 0 should be NULL.

But for these types are expressions I would like to use SUM instead of COUNT.  There are atleast two reasons I know

1 SUM can be used to COUNT as well as to SUM the values. See the below examples

SUM(CASE WHEN gender='M' THEN 1 ELSE 0 END) as male_count,
SUM(CASE WHEN year(trans_date)=2012 THEN trans_amount ELSE 0 END) as total_2012


The first expression COUNTs how many males in the recordset and second expression SUMs the values of the column named trans_amout for the year 2012.

COUNT can be used only for COUNTing as below

COUNT(CASE WHEN gender='M' THEN 1 END) as male_count


2 NULL warning

Using SUM will almost avoid NULL warning. In the first expression the derived values are always 1 and 0 so you will never get any warnings on NULL. In the second example, if the column trans_amount is a NOT NULL column, you will never get a NULL warning but if it is NULLable column and if there are some NULL values for the transaction year 2012, you will get a NULL warning. The NULL warning is as below

Warning: Null value is eliminated by an aggregate or other SET operation.

But using COUNT will most likely result to NULL warning until there are no rows with gender<>'M'.

The NULL warning depends on the data. So use conditional aggregations cleverly.

Tags: 


Madhivanan
2 · 40% · 12993
6
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

6  Comments  

  • hi using

    COUNT(CASE WHEN gender='M' THEN 1 END) as male_count

    give correct count?? as you said count work as count(*).

    and can we do COUNT(CASE WHEN .. THEN 1 ELSE NULL END) is it give correct answer??

    commented on Mar 26 2013 12:38AM
    riks
    1014 · 0% · 26
  • HI Madhivanan,

    Thanks for original Post

    Thanks.

    commented on Mar 26 2013 6:18AM
    Bala Krishna
    83 · 2% · 676
  • @Riks,

    There are 2 things to observe.

    1. If we use COUNT(CASE WHEN gender='M' THEN 1 ELSE 0 END) , RESULT= COUNT(CONDITIONAL PART + ELSE PART) that means like COUNT(*)

    2. If we use SUM(CASE WHEN gender='M' THEN 1 ELSE 0 END) , RESULT= COUNT(CONDITIONAL PART) that means always count the first condition result(Here Gender='M') .

    Hope this will help you.

    Thanks.

    commented on Mar 26 2013 7:04AM
    Bala Krishna
    83 · 2% · 676
  • Hi , Bala

    Thanks for your reply. but if we use COUNT(CASE WHEN gender='M' THEN 1 ELSE NULL END) .

    than is it give correct o/p or not??

    commented on Mar 27 2013 11:22PM
    riks
    1014 · 0% · 26
  • riks, that will give you the correct result. However there will a warning about NULL. If you use SUM(CASE WHEN gender='M' THEN 1 ELSE 0 END), you won't get the NULL warning

    commented on Apr 10 2013 8:25AM
    Madhivanan
    2 · 40% · 12993
  • ok thank you very much Madhivanan

    commented on Apr 10 2013 10:55PM
    riks
    1014 · 0% · 26

Your Comment


Sign Up or Login to post a comment.

"Conditional aggregation - SUM Vs COUNT" rated 5 out of 5 by 6 readers
Conditional aggregation - SUM Vs COUNT , 5.0 out of 5 based on 6 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]