Logo

SQL Server RunningSKEW_S Function

Updated 2023-11-14 15:29:49.507000

Description

Use the scalar function 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.

Return Type

float

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

Examples

In this example, we have 20 rows of data and we want to calculate 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.

rnxySKEW_S xSKEW_S y
1101117NULLNULL
29197NULLNULL
3961210-1.54539252569502
4961030-0.129678815660363
58674-0.404796008910937-0.864550209720427
69580-0.547871643589439-0.179587851309796
791105-0.179812666650057-0.384309637141166
810272-0.183091963166347-0.0994544316100886
994108-0.124285714285715-0.331171026292888
10110940.709070384461684-0.265262411674906
11121851.2958445379977-0.0584746272003931
12115900.8933786819471060.0476461160613627
13112960.6183772957809680.0401051990837308
14100970.6537621839494440.0164309317695287
151241060.625574752271121-0.119264289357361
1692610.731016831476697-0.359228636579172
17921070.828122509715119-0.47105159705444
18139921.16094637809343-0.446870439048321
19951011.24572587647292-0.518472653357385
20901041.30010690278267-0.602170891442928