SQLServerCentral.com / Article Discussions / Article Discussions by Author / Discuss content posted by jkelly / Standard Deviation / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comFri, 30 Jan 2015 07:36:38 GMT20RE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxThanks for clarifying this. As I said it has been a while since I used statistics :-)Mon, 17 Sep 2012 08:03:49 GMTmdv 9731RE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]mdv 9731 (1/28/2011)[/b][hr]I do not agree that the answer is right. Think it is supposed to be 0.the standard deviation σ (sigma) is the square root of the average value of (X − μ)2.Which means σ = sqrt(((1-1)^2)/1) = 0Or in other words std. deviation equals sqrt of (population-avarage) sq / number of population valuesPlease correct me if im wrong on the formula it has been a while since i used my statistics :-D[/quote]What you state is correct for a population standard deviation. Then you devide by the number of samples N, which is one for a single record. You do this with the function STDEVP. When using STDEV however, you take the sample standard deviation in which you devide by N - 1 which would result in a division by zero. Hence the NULL. SELECT STDEV(100)Results in NULLSELECT STDEVP(100)Results in 0See: http://en.wikipedia.org/wiki/Standard_deviationMon, 17 Sep 2012 07:37:56 GMTRon DeijkersRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxThanks for the questionTue, 08 Feb 2011 12:15:04 GMTSQLRNNRRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxThanks for the question. I learn new thing today. Discussion about the topic is really good...Mon, 07 Feb 2011 04:35:17 GMTHardy21RE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxSimple Question but discussion was so detailed that you learn a loads of thing from the discussion :-DFri, 28 Jan 2011 19:40:33 GMTDhruvesh ShahRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxHi Tom, I see that we are on the same page. The only way I can make sense of Microsoft's nomenclature is to remember that I should use STDEVP when I'm applying the function to a data set to calculate the standard deviation of only those data points (i.e., when I'm feeding the function the whole population and not a sample), hence my reliance on the "P for population" mnemonic device, and STDEV when I'm applying the function to a data set that serves as a sample of some larger data set for which I want to estimate the standard deviation. Otherwise, I go crazy trying to remember whether I should be using the "n" or the "n-1" method and which function goes with which!JasonFri, 28 Jan 2011 15:13:00 GMTwolfkilljRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]wolfkillj (1/28/2011)[/b][hr]I think the "P" in STDEVP does stand for "population", as that function returns the actual standard deviation of the entire population (sometimes called the "n" method); hence, the standard deviation of a population consisting of a single value = 0, as STDEVP will return. STDEV returns the standard deviation with Bessel's correction applied (i.e., the estimated population standard deviation or the sample standard deviation, sometimes call the "n-1" method), which is why applying this function to a population consisting of a single value will not work. Can anyone who knows the programmatic bases of STDEV and STDEVP confirm my conclusion?[/quote]Up to a point you are right (certainly the code bases are the way round that you suggest), but STDEVP returns the standard deviation of the sample provided to it, not that of the population from which the sample is drawn. I guess that what's happened is that someone chose the name STDEVP to mean a version of STDEV to which the whole population was provided, rather than a sample, and that's why the code without Bessel's correction has the P in its name. But many statisticians (and other mathematicians) talk about the standard deviation of the sample and the estimated standard deviation of the population, and (at least for distributions thought to be close to normal) instead of using the standard deviation of the sample as an estimate for the population SD they multiply it by N/(N-1) (Bessel's correction) to get a better estimate (or more usually shortcircuit that by dividing by N-1 instead of by N in the first place), so in the usual terminology the population is ony referred to in the corrected case, not in the uncorrected case, so people may be confused by the naming with P included in the name that doesn't try to provide the population (as opposed to sample) standard deviation (as I was).edit: take out superfluous quote tagFri, 28 Jan 2011 14:51:47 GMTTomThomsonRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote]I thought the P in STDEVP stood for "Population", so STDEVP would be the estimated Population Standard Deviation (obtained by applying Bessel's correction to the actual Sample Standard Deviation) and STDEV the actual Sample Standard Deviation (in the SQL 2000 days you could look it up in BoL, so I never bothered to learn it - I wanted those functions rarely enough for it not to be worth making an effort to learn it); when I discovered that which is which isn't documented (at least not in any obvious manner) in BoL for SQL 2008 I just tried to work out which was which based on the name, and got it wrong because the SQL function names are misleading.[/quote]I think the "P" in STDEVP does stand for "population", as that function returns the actual standard deviation of the entire population (sometimes called the "n" method); hence, the standard deviation of a population consisting of a single value = 0, as STDEVP will return. STDEV returns the standard deviation with Bessel's correction applied (i.e., the estimated population standard deviation or the sample standard deviation, sometimes call the "n-1" method), which is why applying this function to a population consisting of a single value will not work. Can anyone who knows the programmatic bases of STDEV and STDEVP confirm my conclusion?Fri, 28 Jan 2011 14:22:09 GMTwolfkilljRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxWell I got this one wrong, mainly because the SQL function naming is a little bizarre: STDEV is the estimated Population Standard Deviation (although there's no P in the name)STDEVP is the actual Sample Standard Deviation (although there is a P in the name)I thought the P in STDEVP stood for "Population", so STDEVP would be the estimated Population Standard Deviation (obtained by applying Bessel's correction to the actual Sample Standard Deviation) and STDEV the actual Sample Standard Deviation (in the SQL 2000 days you could look it up in BoL, so I never bothered to learn it - I wanted those functions rarely enough for it not to be worth making an effort to learn it); when I discovered that which is which isn't documented (at least not in any obvious manner) in BoL for SQL 2008 I just tried to work out which was which based on the name, and got it wrong because the SQL function names are misleading.You can't apply Bessel's correction when your sample has only 1 member, so SQL Server quite correctly refuses to attempt it. Actually it would be better to return an appropriate error (one that specifically means that STDEV has been applied to a singleton sample) than to return NULL. Returning NULL is better than returning a zero-divide error which might be caused by some other part of a select statement - eg if [code]SELECT STDEV((a+c)/b) FROM T where a > 37 [/code]returned a zero-divide error one couldn't tell whether the error occurred because one of the b attributes was zero or because there was at most one row with a > 37. But returning NULL means you don't know whether the sample was empty or a singleton, which is why a specific error should be returned (ideally a specific error should be returned instead of NULL for the empty sample case too, but that is another story, part of a much more general one).Fri, 28 Jan 2011 14:01:51 GMTTomThomsonRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxThanks for the explanation wolfkillj! That makes total sense now.Fri, 28 Jan 2011 10:24:50 GMTUMG DeveloperRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxWhat's going on here is that the STDEV() function in T-SQL returns the sample standard deviation (which for a single value will be undefined, hence the programmatically dictated NULL result) rather than the population standard deviation (which for a single value will be 0). BOL does not clearly state this, which apparently causes some confusion. Another T-SQL function, STDEVP(), returns the population standard deviation. Developers using statistical functions in T-SQL definitely should be aware of this!Fri, 28 Jan 2011 10:12:25 GMTwolfkilljRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxThanks for the question, it is good to know that SQL Server decides to return NULL for the standard deviation for a data value, though I don't think it is alone.Fri, 28 Jan 2011 10:11:56 GMTUMG DeveloperRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxRight answer, wrong explanation. The stdev function is simply programmed to return null when the sample size is less than 2. It has nothing to do with division by zero. Division by zero is an invalid calculation not an unknown result. If it were allowed to perform the zero division the proper response of the function should have been to throw an error; instead they trapped the invalid condition and returned a NULL. I would also argue that this is an inaccurate result since it ignores the fact that an invalid data set is being used without returning the appropriate error.Fri, 28 Jan 2011 07:24:23 GMTBarry McConnellRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxDivision by zero should be ERROR, not null. How come?Fri, 28 Jan 2011 06:13:39 GMTvaleryk2000RE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxThat makes sence.So its really a matter of SQL Server returning NULL instead of #ERROR not having the correct syntax which obviously means that a FROM clause needs to be included.So the point proven is more like SQL Server returning a 'value' instead of an error.And not that it can't calculate :-) (and is has nothing to do with a zero division)'cause my point was that std. dev. is 0 if calculated on a single value no matter what it is (even if that makes no sence)Question good, formula good, Microsoft needs to work on documentationWe're all happyCheersFri, 28 Jan 2011 03:57:47 GMTmdv 9731RE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]sharath.chalamgari (1/28/2011)[/b][hr][quote][b]Koen (da-zero) (1/28/2011)[/b]Then why is the explanation confusing? I find it pretty clear...[/quote]It is Confusing because in his Query the parameter 1 does not mean that Number of samples.if you put 100 inplace of 1 still it gives the same null. as per his explanation it is not a divide by zero errorThe usage of the Function is wrong in the Query.see if we can use some thing like below it gives 0[code="sql"]SELECT STDEV(1) from sys.objects[/code][/quote]Allright, that makes sense. Thanks for the explanation.Fri, 28 Jan 2011 03:48:15 GMTKoen VerbeeckRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]Koen (da-zero) (1/28/2011)[/b]Then why is the explanation confusing? I find it pretty clear...[/quote]It is Confusing because in his Query the parameter 1 does not mean that Number of samples.if you put 100 inplace of 1 still it gives the same null. as per his explanation it is not a divide by zero errorThe usage of the Function is wrong in the Query.see if we can use some thing like below it gives 0[code="sql"]SELECT STDEV(1) from sys.objects[/code]Fri, 28 Jan 2011 03:41:27 GMTsharath.chalamgariRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]mdv 9731 (1/28/2011)[/b][hr]I'm not really going to comment on that answer.Just happy you are not creating any kind of BI og reporting in our company :-)I might be wrong but i still question the result. Even if i know that it doesnt make much practical sence to talk deviation on 1 value.[/quote]The question was about the result of the query (ergo: what does SQL Server do with it).Not what is the theoretical result according to statistics. (however, the explanation might be lacking. I'm (luckily) not a statistician)But don't worry, if I'll do BI or reporting in your company, I will read the requirements thoroughly ;-)Fri, 28 Jan 2011 03:17:11 GMTKoen VerbeeckRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]Koen (da-zero) (1/28/2011)[/b][hr]Then why is the explanation confusing? I find it pretty clear...[/quote]The explanation is "The denominator of the variance for a sample population is n - 1, resulting in division by zero."This suggests it should give an error, and doesn't explain why it results in null instead.I guessed wrong, but I'm not sure what the question was supposed to prove, other than if you apply a function wrongly then you get a meaningless answer!Fri, 28 Jan 2011 03:01:59 GMTToreadorRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxI'm not really going to comment on that answer.Just happy you are not creating any kind of BI og reporting in our company :-)I might be wrong but i still question the result. Even if i know that it doesnt make much practical sence to talk deviation on 1 value.Fri, 28 Jan 2011 02:50:25 GMTmdv 9731RE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]mdv 9731 (1/28/2011)[/b][hr]I do not agree that the answer is right. Think it is supposed to be 0.the standard deviation σ (sigma) is the square root of the average value of (X − μ)2.Which means σ = sqrt(((1-1)^2)/1) = 0Or in other words std. deviation equals sqrt of (population-avarage) sq / number of population valuesPlease correct me if im wrong on the formula it has been a while since i used my statistics :-D[/quote]Even if your statistics say it is 0, SQL Server returns NULL, so the answer is still correct.You can verify it by running the query in SQL Server Management Studio.Fri, 28 Jan 2011 02:34:17 GMTKoen VerbeeckRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxI do not agree that the answer is right. Think it is supposed to be 0.the standard deviation σ (sigma) is the square root of the average value of (X − μ)2.Which means σ = sqrt(((1-1)^2)/1) = 0Or in other words std. deviation equals sqrt of (population-avarage) sq / number of population valuesPlease correct me if im wrong on the formula it has been a while since i used my statistics :-DFri, 28 Jan 2011 02:09:54 GMTmdv 9731RE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxThanks for introducing new topic. I had not heard about it beforeFri, 28 Jan 2011 02:01:02 GMTmohammed moinudheenRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]sharath.chalamgari (1/28/2011)[/b][hr][quote][b]Koen (da-zero) (1/28/2011)[/b][hr][quote][b]sharath.chalamgari (1/28/2011)[/b][hr]but if i cange the Query with some where condition,where i will get only one Row (sample) then the stdevp will results the null value[code="sql"]select stdev(pa_Rates) From Package where pa_PackageID = 1[/code][/quote]The WHERE clause is executed first, so you are still calculating the variance over 1 row, hence the NULL.[/quote]Ya that's what i have explained above.[/quote]Then why is the explanation confusing? I find it pretty clear...Fri, 28 Jan 2011 01:50:49 GMTKoen VerbeeckRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]Koen (da-zero) (1/28/2011)[/b][hr][quote][b]sharath.chalamgari (1/28/2011)[/b][hr]but if i cange the Query with some where condition,where i will get only one Row (sample) then the stdevp will results the null value[code="sql"]select stdev(pa_Rates) From Package where pa_PackageID = 1[/code][/quote]The WHERE clause is executed first, so you are still calculating the variance over 1 row, hence the NULL.[/quote]Ya that's what i have explained above.Fri, 28 Jan 2011 01:45:26 GMTsharath.chalamgariRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspx[quote][b]sharath.chalamgari (1/28/2011)[/b][hr]but if i cange the Query with some where condition,where i will get only one Row (sample) then the stdevp will results the null value[code="sql"]select stdev(pa_Rates) From Package where pa_PackageID = 1[/code][/quote]The WHERE clause is executed first, so you are still calculating the variance over 1 row, hence the NULL.Fri, 28 Jan 2011 01:34:06 GMTKoen VerbeeckRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxI think the Explanation is bit confusing.if you give any numeric value to the function without any from clause it will give NULL.[code="sql"]SELECT STDEV(100)[/code]"The STDEV function uses all of the values to calculate the standard deviation. There must be at least two values in the field or the standard deviation will not be calculated and a NULL is returned."example in my PackageTable the standard deveation of pa_rates column can find using [code="sql"]select stdev(pa_Rates) From Package[/code]for the above Query i will get some resultbut if i cange the Query with some where condition,where i will get only one Row (sample) then the stdevp will results the null value[code="sql"]select stdev(pa_Rates) From Package where pa_PackageID = 1[/code]Fri, 28 Jan 2011 01:24:38 GMTsharath.chalamgariRE: Standard Deviationhttp://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxNice question, really learned something.The BOL page doesn't even mention this subject.Fri, 28 Jan 2011 00:08:39 GMTKoen VerbeeckStandard Deviation http://www.sqlservercentral.com/Forums/Topic1055069-2898-1.aspxComments posted to this topic are about the item [B]<A HREF="/questions/T-SQL/72155/">Standard Deviation </A>[/B]Thu, 27 Jan 2011 21:42:36 GMTjkelly