Updated: 30 June 2013

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

SELECT [westclintech].[wct].[RunningSKEW_P](

<@X, float,>

,<@RowNum, int,>

,<@Id, tinyint,>)

the value passed into the function. *@X* 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 population 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**.

a unique identifier for the RunningSKEW_P calculation. *@Id* allows you to specify multiple moving population skewness 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 population skewness, use the MovingSKEW_P function.

· To calculate the population skewness for an entire data set, use the SKEWNESS_P function.

· If *@RowNum* is equal to 1, RunningSKEW_P 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

In this example, we have 20 rows of data and we want to caculate the population skewness of x and y starting from the first row. Note that the *@Id* value for each RunningSKEW_P column is different.

SELECT rn

,x

,y

,wct.RunningSKEW_P(x,rn,NULL) as [SKEW_P x]

,wct.RunningSKEW_P(y,rn,1) as [SKEW_P 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_P x SKEW_P y

---- ----------- ----------- ---------------------- ----------------------

1 101 117 NULL NULL

2 91 97 NULL NULL

3 96 121 0 -0.630903856710624

4 96 103 0 -0.0748700991297024

5 86 74 -0.271545417883639 -0.579957911668972

6 95 80 -0.400108877081822 -0.131152422955012

7 91 105 -0.138728484148604 -0.296501322167989

8 102 72 -0.146800156380065 -0.0797409447174197

9 94 108 -0.10253048327205 -0.273202158161578

10 110 94 0.59794064967205 -0.223688906272518

11 121 85 1.11198535961314 -0.0501780325100918

12 115 90 0.777586334170716 0.0414706209967757

13 112 96 0.544607881006138 0.035320843179948

14 100 97 0.581520926533674 0.014615300335522

15 124 106 0.561193868737353 -0.106990232108443

16 92 61 0.660617069958198 -0.32463352295545

17 92 107 0.753184539376549 -0.428425475684856

18 139 92 1.0618701934365 -0.408734122873594

19 95 101 1.14513939145121 -0.476608433655409

20 90 104 1.20049338388635 -0.556032868988637