SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Gotcha! SQL Aggregate Functions and NULL


Gotcha! SQL Aggregate Functions and NULL

Author
Message
Mike C
Mike C
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4667 Visits: 1172
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/mcoles/gotchasqlaggregatefunctionsandnull.asp
jwainz
jwainz
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 40
I learned to use COUNT(1) instead of COUNT(*), being told it was faster. This should return the same result as COUNT(*), unaffedted by NULLs.



Mike C
Mike C
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4667 Visits: 1172

Thanks for the feedback!

I'm not sure that COUNT(1) is any faster than COUNT(*). If you look at the query plan for the sample query in the article:

SELECT COUNT(*) AS NULLRows
FROM [Northwind].[dbo].[suppliers]

It turns out it's exactly the same as the query plan for the modified query:

SELECT COUNT(1) AS NULLRows
FROM [Northwind].[dbo].[suppliers]

If you take a look at the query plans, pay special attention to the "Stream Aggregate/Aggregate Step". With both query plans, the argument for this step is "[exprnnnn]=COUNT(*)". It looks as if SQL Server just converts "COUNT(1)" into "COUNT(*)" for you.

There might be a speed difference in choosing COUNT(*) or COUNT(column) depending on your table indexes. COUNT(*) allows SQL Server to automatically choose the best index for the job. If you think SQL Server is not choosing the best index for the job, you can always specify COUNT(column) to force SQL Server to re-consider its index usage.

Even if there were a slight savings in speed with using COUNT(1), I would recommend sticking with the ANSI-defined syntax, COUNT(*). There's no guarantee that non-ANSI syntax will work on other platforms; or even different versions of the same platform.


Merrill Aldrich
Merrill Aldrich
Old Hand
Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)Old Hand (326 reputation)

Group: General Forum Members
Points: 326 Visits: 137
Great article - thank you for composing such a clear and simple explanation.
Mike C
Mike C
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4667 Visits: 1172
Thanks for the feedback! I'm glad you found it helpful.
Kumaravel-229312
Kumaravel-229312
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 6

Simple and easily understandable

Thanks. Expecting more articles like this


Mike C
Mike C
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4667 Visits: 1172
Thanks, your posts help me refine the style I will use in future article submissions. I appreciate your feedback!
sushila
sushila
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6085 Visits: 639
Mike - very comprehensive and love the presentation - simple and direct!







**ASCII stupid question, get a stupid ANSI !!!**
Mike C
Mike C
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4667 Visits: 1172

Thanks, I'm glad you enjoyed it!


Mike C
Mike C
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4667 Visits: 1172

Someone e-mailed me a question directly (name and address withheld on request). The question was a good one, so I thought I'd address it with a post here:

"Why would you assume NULL values are zeroes [by using COALESCE(column, 0)] in your AVG() function calls? It seems like that would throw your answer even further off!"

That's an excellent question! On the face of it, it doesn't appear to make much sense for normal averaging. But bear with me as I walk through this scenario:

You have a room with 20 football players in it, and you have to calculate the average weight for the room. If 10 players refuse to give their weight (i.e., NULL), how do you estimate the average?

Using AVG(weight) and eliminating NULLs gives us an average at a point in time when we only have half the data. This is standard practice, and it is 'precise', but it is not 'accurate'. The fact that it is not accurate should be noted on reports generated using this data. After all, this average accounts for only 50% of the players in the room; and our final result, once we get the remaining players' weights could be heavily weighted (no pun intended) in one direction or the other. I.e., if our average is 150 pounds with 10 players responding, it could very well jump to 250 or higher by the time we get everyone's weights included in the final calculation.

Can we be more accurate than this? The answer is yes, but we will become less 'precise' in the process.

Let's say that we have a tid-bit of information about the football players in the room. We are told in advance that nobody is over 300 pounds, and we determine (obviously) that nobody is less than 0 pounds. Since this is a football team, management wants all the players to be bulky; therefore, the greater the weight, the better. These are our best-case/worst-case scenarios. Using these two numbers, with COALESCE(), we can come up with best-case/worst-case scenario averages:

SELECT AVG(COALESCE(weight, 300)) AS BestCase

SELECT AVG(COALESCE(weight, 0)) AS WorstCase

This will give us a range that we know our final average will fall between. We can make our answer more precise if we can narrow our limits (i.e., no one weighs less than 100 pounds).

We actually use these type of range calculations all the time without even thinking about them. When someone asks how much something costs ("$5 to $10"), how much profit the company will make this year ("between $1.6 mil. and $1.8 mil."), or what time will we arrive ("between 5:00 and 5:30").

So to answer the question, AVG(COALESCE()) is useful when trying to determine best-case average, worst-case average, or when determining a range for our averages.


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