Login     Register

        Contact Us     Search

XLeratorDB/windowing Documentation

SQL Server running DEVSQ function


Updated: 31 Oct 2012

Use RunningDEVSQ to calculate the sum of squares of deviations of column values from their sample mean in an ordered resultant table, without the need for a self-join. The sum of squares of deviations 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].[RunningDEVSQ](
  <@Val, float,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)
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 RunningDEVSQ calculation. @Id allows you to specify multiple RunningDEVSQ 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 sum of squares deviation, use the MovingDEVSQ function.
·         If @RowNum is equal to 1, RunningDEVSQ is equal to 0.
·         @RowNum must be in ascending order.
·         To calculate a single sum of squares deviation for a set of data, use the DEVSQ function.
·         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 sample data from a population with a mean of 100 and a standard deviation of 15.
,wct.RunningDEVSQ(x,rn,NULL) as [DEVSQ]
      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                  DEVSQ
----------- --------------------------------------- ----------------------
          1                                 85.2968                      0
          2                                 88.2566       4.38020802000028
          3                                100.1934       124.385433946668
          4                                116.3052       595.247808399996
          5                                109.6867       713.806985752005
          6                                130.3847       1485.81409745335
          7                                 76.5458       2180.79610035717
          8                                 99.5511       2182.51512771877
          9                                101.5546       2183.05186205559
         10                                114.3180       2345.96392634903
         11                                100.2686       2349.38781514546
         12                                110.5982       2416.63905324921
         13                                 91.4181       2535.10292059694
         14                                118.5804       2794.23302841429
         15                                126.6649       3313.90629879603
         16                                103.8977       3314.42501562004
         17                                 82.2819       3783.01481601768
         18                                123.3369       4162.85005420446
         19                                 98.9415        4191.0424129611
         20                                 89.1731       4402.98455185802

Copyright 2008-2013 WestClinTech LLC         Privacy Policy        Terms of Service