Updated: 31 Oct 2012

Use RunningSTDEVP to calculate the population standard deviation of column values in an ordered resultant table, without the need for a self-join. The population standard deviation is calculated over all the values from the first value to the last value in the ordered group or partition. If the column values are presented to the functions out of order, an error message will be generated.

SELECT [Example].[wct].[RunningSTDEVP](

<@Val, float,>

,<@RowNum, int,>

,<@Id, tinyint,>)

GO

the value passed into the function. *@Val* is an expression of type **float** or of a type that can be implicitly converted to **float**.

the number of the row within the group for which the sum is being calculated. If *@RowNum* for the current row in a set is less than or equal to the previous *@RowNum* and *@RowNum* is not equal to 1, an error message will be generated.* @RowNum* is an expression of type **int** or of a type that can be implicitly converted to **int**.

a unique identifier for the RunningSTDEVP calculation. *@Id* allows you to specify multiple RunningSTDEVP calculations within a resultant table. *@Id* is an expression of type **tinyint** or of a type that can be implicitly converted to **tinyint**.

· If *@Id* is NULL then *@Id* = 0.

· To calculate moving standard deviations, use the MovingSTDEVP function.

· If *@RowNum* is equal to 1, RunningSTDEVP is equal to zero

· *@RowNum* must be in ascending order.

· There may be cases where the order in which the data are returned to the function and the order in which the results are returned are different, generally due to parallelism. You can use OPTION(MAXDOP 1) or OPTION(MAXDOP 1,FORCE ORDER) to help eliminate this problem.

In this example, we have 20 rows of population data.

SELECT rn

,x

,wct.RunningSTDEVP(x,rn,NULL) as [STDEVP]

FROM (

SELECT 1,85.2968 UNION ALL

SELECT 2,88.2566 UNION ALL

SELECT 3,100.1934 UNION ALL

SELECT 4,116.3052 UNION ALL

SELECT 5,109.6867 UNION ALL

SELECT 6,130.3847 UNION ALL

SELECT 7,76.5458 UNION ALL

SELECT 8,99.5511 UNION ALL

SELECT 9,101.5546 UNION ALL

SELECT 10,114.318 UNION ALL

SELECT 11,100.2686 UNION ALL

SELECT 12,110.5982 UNION ALL

SELECT 13,91.4181 UNION ALL

SELECT 14,118.5804 UNION ALL

SELECT 15,126.6649 UNION ALL

SELECT 16,103.8977 UNION ALL

SELECT 17,82.2819 UNION ALL

SELECT 18,123.3369 UNION ALL

SELECT 19,98.9415 UNION ALL

SELECT 20,89.1731

) s(rn,x)

This produces the following result.

rn x STDEVP

----------- --------------------------------------- ----------------------

1 85.2968 0

2 88.2566 1.4799

3 100.1934 6.43908466441896

4 116.3052 12.1988504417424

5 109.6867 11.9482800917287

6 130.3847 15.7364444176214

7 76.5458 17.6505608990485

8 99.5511 16.51709390192

9 101.5546 15.5743801804814

10 114.3180 15.3165398388441

11 100.2686 14.6144007905822

12 110.5982 14.1910742289216

13 91.4181 13.9645235138545

14 118.5804 14.1275643144232

15 126.6649 14.8636162015305

16 103.8977 14.3927608010503

17 82.2819 14.9174489539987

18 123.3369 15.2075457407105

19 98.9415 14.8519756136862

20 89.1731 14.8374265825614