Login     Register

        Contact Us     Search

XLeratorDB/windowing Documentation

SQL Server moving DEVSQ function


MovingDEVSQ

Updated: 31 Oct 2012


Use MovingDEVSQ 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 deviations 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.
Syntax
SELECT [Example].[wct].[MovingDEVSQ](
  <@Val, float,>
 ,<@Offset, int,>
 ,<@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.
@Offset
specifies the window size. @Offset is an expression of type int or of a type that can be implicitly converted to int.
@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 MovingDEVSQ calculation. @Id allows you to specify multiple MovingDEVSQ 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 running sum of squares deviation over an entire dataset or partition, use the RunningDEVSQ function.
·         If @RowNum is equal to 1, MovingDEVSQ is equal to 0.
·         @RowNum must be in ascending order.
·         To calculate a single sum of squares deviation value for an entire 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.
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 cast(x as money) as x
,cast(wct.MovingDEVSQ(x,5,ROW_NUMBER() OVER (ORDER BY x ASC),NULL) as money) as [DEVSQ]
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)
ORDER BY x ASC
 
 This produces the following result.
                    x                 DEVSQ
--------------------- ---------------------
              76.5458                  0.00
              82.2819               16.4514
              85.2968               39.5242
              88.2566               75.0432
              89.1731              104.5952
              91.4181              146.6894
              98.9415              163.7979
              99.5511              172.4118
             100.1934              154.4619
             100.2686              122.5732
             101.5546               66.6359
             103.8977               15.8035
             109.6867               73.4877
             110.5982              109.4727
              114.318              157.8386
             116.3052              165.2117
             118.5804              139.8529
             123.3369              130.7684
             126.6649              174.5738
             130.3847              196.0214
 

 



Copyright 2008-2013 WestClinTech LLC         Privacy Policy        Terms of Service