Logo

MovingSTDEVP

Updated 2023-11-13 21:48:10.050000

Syntax

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

Description

Use the scalar function MovingSTDEVP 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 for each value from the first value in the window to the last value in the window. If the column values are presented to the functions out of order, an error message will be generated.

Arguments

@Id

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

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

@Offset

specifies the window size. @Offset is an expression of type int or of a type that can be implicitly converted to int.

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

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

To calculate moving standard deviations from the beginning of a dataset or a partition, use the RunningSTDEVP function.

If @RowNum is equal to 1, MovingSTDEVP 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.

Examples

In this example, we have 20 rows of data from a population and we want to calculate the population standard deviation for the previous 4 row and the current row in a window where the x-values are sorted in ascending order.

SELECT cast(x as money) as x,

       cast(wct.MovingSTDEVP(x, 4, ROW_NUMBER() OVER (ORDER BY x ASC), NULL) as 

                 money) 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.

{"columns":[{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"STDEVP","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"x":"76.5458","STDEVP":"0.00"},{"x":"82.2819","STDEVP":"2.8681"},{"x":"85.2968","STDEVP":"3.6297"},{"x":"88.2566","STDEVP":"4.3314"},{"x":"89.1731","STDEVP":"4.5737"},{"x":"91.4181","STDEVP":"3.1804"},{"x":"98.9415","STDEVP":"4.6022"},{"x":"99.5511","STDEVP":"4.8326"},{"x":"100.1934","STDEVP":"4.6118"},{"x":"100.2686","STDEVP":"3.3628"},{"x":"101.5546","STDEVP":"0.8714"},{"x":"103.8977","STDEVP":"1.5457"},{"x":"109.6867","STDEVP":"3.546"},{"x":"110.5982","STDEVP":"4.2089"},{"x":"114.318","STDEVP":"4.6451"},{"x":"116.3052","STDEVP":"4.2773"},{"x":"118.5804","STDEVP":"3.3621"},{"x":"123.3369","STDEVP":"4.2572"},{"x":"126.6649","STDEVP":"4.5471"},{"x":"130.3847","STDEVP":"5.1462"}]}