Logo

SQL Server RunningSTDEV Function

Updated 2023-11-14 15:33:38.323000

Description

Use the scalar function RunningSTDEV to calculate the sample standard deviation of column values in an ordered resultant table, without the need for a self-join. The 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.

Syntax

SELECT [westclintech].[wct].[RunningSTDEV](
  <@Val, float,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)

Arguments

@Val

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

@RowNum

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.

@Id

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

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

To calculate moving standard deviations, use the MovingSTDEV function.

If @RowNum is equal to 1, RunningSTDEV is equal to NULL

@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.

Examples

In this example, we have 20 rows of sample data from a population with a mean of 100 and a standard deviation of 15.

SELECT rn,
       x,
       wct.RunningSTDEV(x, rn, NULL) as [STDEV]
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.

rnxSTDEV
185.2968NULL
288.25662.09289465095594
3100.19347.88623591920336
4116.305214.086019172688
5109.686713.3585832496564
6130.384717.238411164915
776.545819.0647847280317
899.551117.6575161160451
9101.554616.5191247575937
10114.318016.1450505878186
11100.268615.3277128598674
12110.598214.8220936235839
1391.418114.5347368987681
14118.580414.6608663518127
15126.664915.3853044419388
16103.897714.8647794369106
1782.281915.376554425524
18123.336915.648435468131
1998.941515.2589544074601
2089.173115.2228784743608