Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

Gotcha! SQL Aggregate Functions and NULL Expand / Collapse
Author
Message
Posted Wednesday, June 22, 2005 3:47 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/mcoles/gotchasqlaggregatefunctionsandnull.asp
Post #193256
Posted Tuesday, July 05, 2005 6:41 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 07, 2013 11:44 AM
Points: 51, 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.


Post #196943
Posted Tuesday, July 05, 2005 8:13 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132

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.

Post #196975
Posted Tuesday, July 05, 2005 9:39 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, March 23, 2014 12:58 PM
Points: 126, Visits: 136
Great article - thank you for composing such a clear and simple explanation.
Post #197011
Posted Tuesday, July 05, 2005 3:50 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
Thanks for the feedback!  I'm glad you found it helpful.
Post #197181
Posted Friday, July 08, 2005 9:42 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, December 21, 2011 4:25 AM
Points: 46, Visits: 6

Simple and easily understandable

Thanks. Expecting more articles like this

 

Post #199032
Posted Sunday, July 10, 2005 11:11 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
Thanks, your posts help me refine the style I will use in future article submissions.  I appreciate your feedback!
Post #199120
Posted Sunday, July 10, 2005 11:42 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 31, 2014 10:05 AM
Points: 2,553, Visits: 559
Mike - very comprehensive and love the presentation - simple and direct!







**ASCII stupid question, get a stupid ANSI !!!**
Post #199122
Posted Monday, July 11, 2005 2:50 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132

Thanks, I'm glad you enjoyed it!

Post #199597
Posted Monday, July 11, 2005 3:31 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132

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.

Post #199614
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse