RunningSKEW_S
Updated: 30 June 2013
Use RunningSKEW_S to calculate the samples skewness of column values in an ordered resultant table, without the need for a self-join. The sample skewness 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 [westclintech].[wct].[RunningSKEW_S](
<@X, float,>
,<@RowNum, int,>
,<@Id, tinyint,>)
Arguments
@X
the value passed into the function. @X 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 sample skewness 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 RunningSKEW_S calculation. @Id allows you to specify multiple running sample skewness 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 sample skewness, use the MovingSKEW_S function.
· To calculate the samples skewness for an entire data set, use the SKEWNESS_S function.
· If @RowNum is equal to 1, RunningSKEW_S 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
Example
In this example, we have 20 rows of data and we want to caculate the sample skewness of x and y starting from the first row. Note that the @Id value for each RunningSKEW_S column is different.
SELECT rn
,x
,y
,wct.RunningSKEW_S(x,rn,NULL) as [SKEW_S x]
,wct.RunningSKEW_S(y,rn,1) as [SKEW_S y]
FROM (
SELECT 1,101,117 UNION ALL
SELECT 2,91,97 UNION ALL
SELECT 3,96,121 UNION ALL
SELECT 4,96,103 UNION ALL
SELECT 5,86,74 UNION ALL
SELECT 6,95,80 UNION ALL
SELECT 7,91,105 UNION ALL
SELECT 8,102,72 UNION ALL
SELECT 9,94,108 UNION ALL
SELECT 10,110,94 UNION ALL
SELECT 11,121,85 UNION ALL
SELECT 12,115,90 UNION ALL
SELECT 13,112,96 UNION ALL
SELECT 14,100,97 UNION ALL
SELECT 15,124,106 UNION ALL
SELECT 16,92,61 UNION ALL
SELECT 17,92,107 UNION ALL
SELECT 18,139,92 UNION ALL
SELECT 19,95,101 UNION ALL
SELECT 20,90,104
)n(rn,x,y)
This produces the following result.
rn x y SKEW_S x SKEW_S y
---- ----------- ----------- ---------------------- ----------------------
1 101 117 NULL NULL
2 91 97 NULL NULL
3 96 121 0 -1.54539252569502
4 96 103 0 -0.129678815660363
5 86 74 -0.404796008910937 -0.864550209720427
6 95 80 -0.547871643589439 -0.179587851309796
7 91 105 -0.179812666650057 -0.384309637141166
8 102 72 -0.183091963166347 -0.0994544316100886
9 94 108 -0.124285714285715 -0.331171026292888
10 110 94 0.709070384461684 -0.265262411674906
11 121 85 1.2958445379977 -0.0584746272003931
12 115 90 0.893378681947106 0.0476461160613627
13 112 96 0.618377295780968 0.0401051990837308
14 100 97 0.653762183949444 0.0164309317695287
15 124 106 0.625574752271121 -0.119264289357361
16 92 61 0.731016831476697 -0.359228636579172
17 92 107 0.828122509715119 -0.47105159705444
18 139 92 1.16094637809343 -0.446870439048321
19 95 101 1.24572587647292 -0.518472653357385
20 90 104 1.30010690278267 -0.602170891442928