Logo

RunningSKEW_S

Updated 2023-11-14 15:29:49.507000

Syntax

SELECT [westclintech].[wct].[RunningSKEW_S](
  <@X, float,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)

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.

Arguments

@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.

@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.

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.

{"columns":[{"field":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"SKEW_S x"},{"field":"SKEW_S y"}],"rows":[{"rn":"1","x":"101","y":"117","SKEW_S x":"NULL","SKEW_S y":"NULL"},{"rn":"2","x":"91","y":"97","SKEW_S x":"NULL","SKEW_S y":"NULL"},{"rn":"3","x":"96","y":"121","SKEW_S x":"0","SKEW_S y":"-1.54539252569502"},{"rn":"4","x":"96","y":"103","SKEW_S x":"0","SKEW_S y":"-0.129678815660363"},{"rn":"5","x":"86","y":"74","SKEW_S x":"-0.404796008910937","SKEW_S y":"-0.864550209720427"},{"rn":"6","x":"95","y":"80","SKEW_S x":"-0.547871643589439","SKEW_S y":"-0.179587851309796"},{"rn":"7","x":"91","y":"105","SKEW_S x":"-0.179812666650057","SKEW_S y":"-0.384309637141166"},{"rn":"8","x":"102","y":"72","SKEW_S x":"-0.183091963166347","SKEW_S y":"-0.0994544316100886"},{"rn":"9","x":"94","y":"108","SKEW_S x":"-0.124285714285715","SKEW_S y":"-0.331171026292888"},{"rn":"10","x":"110","y":"94","SKEW_S x":"0.709070384461684","SKEW_S y":"-0.265262411674906"},{"rn":"11","x":"121","y":"85","SKEW_S x":"1.2958445379977","SKEW_S y":"-0.0584746272003931"},{"rn":"12","x":"115","y":"90","SKEW_S x":"0.893378681947106","SKEW_S y":"0.0476461160613627"},{"rn":"13","x":"112","y":"96","SKEW_S x":"0.618377295780968","SKEW_S y":"0.0401051990837308"},{"rn":"14","x":"100","y":"97","SKEW_S x":"0.653762183949444","SKEW_S y":"0.0164309317695287"},{"rn":"15","x":"124","y":"106","SKEW_S x":"0.625574752271121","SKEW_S y":"-0.119264289357361"},{"rn":"16","x":"92","y":"61","SKEW_S x":"0.731016831476697","SKEW_S y":"-0.359228636579172"},{"rn":"17","x":"92","y":"107","SKEW_S x":"0.828122509715119","SKEW_S y":"-0.47105159705444"},{"rn":"18","x":"139","y":"92","SKEW_S x":"1.16094637809343","SKEW_S y":"-0.446870439048321"},{"rn":"19","x":"95","y":"101","SKEW_S x":"1.24572587647292","SKEW_S y":"-0.518472653357385"},{"rn":"20","x":"90","y":"104","SKEW_S x":"1.30010690278267","SKEW_S y":"-0.602170891442928"}]}