Logo

RunningSKEW_P

Updated 2023-11-14 15:28:24.127000

Syntax

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

Description

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

Arguments

@Id

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.

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

Return Type

float

Remarks

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.

Examples

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

{"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_P x"},{"field":"SKEW_P y"}],"rows":[{"rn":"1","x":"101","y":"117","SKEW_P x":"NULL","SKEW_P y":"NULL"},{"rn":"2","x":"91","y":"97","SKEW_P x":"NULL","SKEW_P y":"NULL"},{"rn":"3","x":"96","y":"121","SKEW_P x":"0","SKEW_P y":"-0.630903856710624"},{"rn":"4","x":"96","y":"103","SKEW_P x":"0","SKEW_P y":"-0.0748700991297024"},{"rn":"5","x":"86","y":"74","SKEW_P x":"-0.271545417883639","SKEW_P y":"-0.579957911668972"},{"rn":"6","x":"95","y":"80","SKEW_P x":"-0.400108877081822","SKEW_P y":"-0.131152422955012"},{"rn":"7","x":"91","y":"105","SKEW_P x":"-0.138728484148604","SKEW_P y":"-0.296501322167989"},{"rn":"8","x":"102","y":"72","SKEW_P x":"-0.146800156380065","SKEW_P y":"-0.0797409447174197"},{"rn":"9","x":"94","y":"108","SKEW_P x":"-0.10253048327205","SKEW_P y":"-0.273202158161578"},{"rn":"10","x":"110","y":"94","SKEW_P x":"0.59794064967205","SKEW_P y":"-0.223688906272518"},{"rn":"11","x":"121","y":"85","SKEW_P x":"1.11198535961314","SKEW_P y":"-0.0501780325100918"},{"rn":"12","x":"115","y":"90","SKEW_P x":"0.777586334170716","SKEW_P y":"0.0414706209967757"},{"rn":"13","x":"112","y":"96","SKEW_P x":"0.544607881006138","SKEW_P y":"0.035320843179948"},{"rn":"14","x":"100","y":"97","SKEW_P x":"0.581520926533674","SKEW_P y":"0.014615300335522"},{"rn":"15","x":"124","y":"106","SKEW_P x":"0.561193868737353","SKEW_P y":"-0.106990232108443"},{"rn":"16","x":"92","y":"61","SKEW_P x":"0.660617069958198","SKEW_P y":"-0.32463352295545"},{"rn":"17","x":"92","y":"107","SKEW_P x":"0.753184539376549","SKEW_P y":"-0.428425475684856"},{"rn":"18","x":"139","y":"92","SKEW_P x":"1.0618701934365","SKEW_P y":"-0.408734122873594"},{"rn":"19","x":"95","y":"101","SKEW_P x":"1.14513939145121","SKEW_P y":"-0.476608433655409"},{"rn":"20","x":"90","y":"104","SKEW_P x":"1.20049338388635","SKEW_P y":"-0.556032868988637"}]}