Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Aggregates in T-SQL


Aggregates in T-SQL

Author
Message
venus.pvr
venus.pvr
SSC Veteran
SSC Veteran (201 reputation)SSC Veteran (201 reputation)SSC Veteran (201 reputation)SSC Veteran (201 reputation)SSC Veteran (201 reputation)SSC Veteran (201 reputation)SSC Veteran (201 reputation)SSC Veteran (201 reputation)

Group: General Forum Members
Points: 201 Visits: 558
Comments posted to this topic are about the item Aggregates in T-SQL
Lokesh Vij
Lokesh Vij
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1790 Visits: 1599
Good Question!

And very rightly pointed out in the explanation that aggregate functions does not take NULL values into account. This is the reason you get the following warning when using aggregate functions with NULL values if ANSI_WARNINGS are enabled.
Warning: Null value is eliminated by an aggregate or other SET operation.


~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


okbangas
okbangas
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1065 Visits: 1387
Good question indeed. I should have spotted that the aggregates will fail due to untyped NULL. If the null value in the last statement were typed, it would work just fine.



Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

Concatenating Row Values in Transact-SQL
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19547 Visits: 13250
Easy question. I didn't even had to check to 3rd query, because the second one gave (NULL,0) and there was only one answer in the list with that option :-)

The explanation however is wrong as pointed out by Ole Kristian. This works perfectly:


SELECT ISNULL(SUM(CONVERT(INT,NULL)),0) as sum_col1
, ISNULL(COUNT(CONVERT(INT,NULL)),0) as cnt_col1
FROM #TestAggregates



Furthermore, the explanation doesn't explain the results of the other two queries. But I guess those are self-explanatory if you read the BOL pages on COUNT and SUM.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23554 Visits: 18275
Thanks for the question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Carlo Romagnano
Carlo Romagnano
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3864 Visits: 3276
Koen Verbeeck (12/20/2012)
Easy question. I didn't even had to check to 3rd query, because the second one gave (NULL,0) and there was only one answer in the list with that option :-)

The explanation however is wrong as pointed out by Ole Kristian. This works perfectly:


SELECT ISNULL(SUM(CONVERT(INT,NULL)),0) as sum_col1
, ISNULL(COUNT(CONVERT(INT,NULL)),0) as cnt_col1
FROM #TestAggregates



Furthermore, the explanation doesn't explain the results of the other two queries. But I guess those are self-explanatory if you read the BOL pages on COUNT and SUM.

+1

I run on tuttopodismo
Yggaz
Yggaz
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 189
There is just one variant of one aggregate function that DOES take nulls into account: COUNT(*).

However, it is uncompatible with DISTINCT clause.

SELECT COUNT(col1) as cnt_col1
, COUNT(*) as cnt_asterisk
FROM #testAggregates

(8, 10)
Lokesh Vij
Lokesh Vij
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1790 Visits: 1599
Yggaz (12/21/2012)
There is just one variant of one aggregate function that DOES take nulls into account: COUNT(*).

However, it is uncompatible with DISTINCT clause.

SELECT COUNT(col1) as cnt_col1
, COUNT(*) as cnt_asterisk
FROM #testAggregates

(8, 10)



I do not agree with this. COUNT(*) only considers counting the number of rows. It does not even bother to take a look at the columns and there values. Hence COUNT(*) is not the variant.

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6570 Visits: 7196
Nice question to end the week
Thanks

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
udayroy15
udayroy15
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 100
Regarding 3 rd query:-

"Operand data type void type is invalid for sum operator."
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search