Login     Register

        Contact Us     Search

XLeratorDB/windowing Documentation

SQL Server running VAR function


RunningVAR

Updated: 31 Oct 2012


Use RunningVAR to calculate the sample variance of column values in an ordered resultant table, without the need for a self-join. The sample 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.
Syntax
SELECT [Example].[wct].[RunningVAR](
  <@Val, float,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)
GO
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 RunningVAR calculation. @Id allows you to specify multiple RunningVAR calculations within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.
Remarks
·         If @Id is NULL then @Id = 0.
·         To calculate moving variances, use the MovingVAR function.
·         If @RowNum is equal to 1, RunningVARis 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.
Example
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.RunningVAR(x,rn,NULL) as [VAR]
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                    VAR
----------- --------------------------------------- ----------------------
          1                                 85.2968                   NULL
          2                                 88.2566             4.38020802
          3                                100.1934       62.1927169733332
          4                                116.3052       198.415936133333
          5                                109.6867          178.451746438
          6                                130.3847       297.162819490666
          7                                 76.5458        363.46601672619
          8                                 99.5511       311.787875388393
          9                                101.5546       272.881482756944
         10                                114.3180       260.662658483222
         11                                100.2686       234.938781514545
         12                                110.5982       219.694459386288
         13                                 91.4181        211.25857671641
         14                                118.5804       214.941002185714
         15                                126.6649       236.707592771143
         16                                103.8977          220.961667708
         17                                 82.2819       236.438426001103
         18                                123.3369       244.873532600261
         19                                 98.9415       232.835689608947
         20                                 89.1731       231.736029045158
 


Copyright 2008-2024 Westclintech LLC         Privacy Policy        Terms of Service