Logo

RunningVARP

Updated 2023-11-14 15:45:31.387000

Syntax

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

Description

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

Arguments

@Id

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

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

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

To calculate moving variances, use the MovingVARP function.

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

SELECT rn,
       x,
       wct.RunningVARP(x, rn, NULL) as [VARP]
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":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"VARP","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"rn":"1","x":"85.2968","VARP":"0"},{"rn":"2","x":"88.2566","VARP":"2.19010401"},{"rn":"3","x":"100.1934","VARP":"41.4618113155555"},{"rn":"4","x":"116.3052","VARP":"148.8119521"},{"rn":"5","x":"109.6867","VARP":"142.7613971504"},{"rn":"6","x":"130.3847","VARP":"247.635682908889"},{"rn":"7","x":"76.5458","VARP":"311.54230005102"},{"rn":"8","x":"99.5511","VARP":"272.814390964844"},{"rn":"9","x":"101.5546","VARP":"242.561318006173"},{"rn":"10","x":"114.3180","VARP":"234.5963926349"},{"rn":"11","x":"100.2686","VARP":"213.580710467769"},{"rn":"12","x":"110.5982","VARP":"201.386587770764"},{"rn":"13","x":"91.4181","VARP":"195.007916968994"},{"rn":"14","x":"118.5804","VARP":"199.588073458163"},{"rn":"15","x":"126.6649","VARP":"220.9270865864"},{"rn":"16","x":"103.8977","VARP":"207.15156347625"},{"rn":"17","x":"82.2819","VARP":"222.530283295156"},{"rn":"18","x":"123.3369","VARP":"231.269447455802"},{"rn":"19","x":"98.9415","VARP":"220.581179629529"},{"rn":"20","x":"89.1731","VARP":"220.1492275929"}]}