Logo

SQL Server MovingKURT_P Function

Updated 2023-11-13 21:25:20.260000

Description

Use the scalar function MovingKURT_P to calculate the population kurtosis of column values in an ordered resultant table, without the need for a self-join. The population kurtosis is calculated for each value from the first value in the window to the last value in the window. If the column values are presented to the functions out of order, an error message will be generated.

Syntax

SELECT [westclintech].[wct].[MovingKURT_P](
  <@Val, float,>
 ,<@Offset, int,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>
 ,<@Exact, bit,>)

Arguments

@Val

the value passed into the function. @Val is an expression of type float or of a type that can be implicitly converted to float.

@Offset

specifies the window size. @Offset is an expression of type int or of a type that can be implicitly converted to int.

@RowNum

the number of the row within the group for which the population kurtosis 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 MovingKURT_P calculation. @Id allows you to specify multiple moving population kurtosis calculations within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.

@Exact

a bit value which tells the function whether or not to return a NULL value if the number of rows in the window is smaller the @Offset value. If @Exact is 'True' and the number of rows in the window is less the @Offset then a NULL is returned. @Exact is an expression of type bit or of a type that can be implicitly converted to bit.

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

To calculate the population kurtosis from the beginning of a dataset or a partition, use the RunningKURT_P function.

To calculate the population kurtosis for an entire data set or for an entire group within a data set use the KURTOSIS_P function.

If @RowNum is equal to 1, MovingKURT_P is equal to zero

@RowNum must be in ascending order.

If @Exact IS NULL then @Exact = 'True'

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 kurtosis of x and y over a window of 10 rows. Note that the @Id value for each MovingKURT_P column is different. Since @Exact is NULL, NULL is returned when the window size is less than @Offset .

SELECT rn,
       x,
       y,
       wct.MovingKURT_P(x, 10, ROW_NUMBER() OVER (ORDER BY RN), NULL, NULL) as [KURT_P x],
       wct.MovingKURT_P(y, 10, ROW_NUMBER() OVER (ORDER BY RN), 1, NULL) as [KURT_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.

rnxyKURT_P xKURT_P y
1101117NULLNULL
29197NULLNULL
396121NULLNULL
496103NULLNULL
58674NULLNULL
69580NULLNULL
791105NULLNULL
810272NULLNULL
994108NULLNULL
1011094-0.0343734328383585-1.19062985993896
11121850.422858034963324-1.04488822774135
1211590-0.818610577428787-1.01713153752299
1311296-1.26556029586073-1.2906972012198
1410097-1.20386093172745-1.14102818165708
15124106-1.34808680374716-0.85107152806444
169261-1.37425975149999-0.322336560384266
1792107-1.38100802479503-0.367905182528677
1813992-0.8097419156142911.21864906572765
1995101-0.7890174897119341.61602202341904
2090104-0.9702380952380951.48096423857436

This example uses the same data as the previous example, however @Exact has been set to 'FALSE' .

SELECT rn,
       x,
       y,
       wct.MovingKURT_P(x, 10, ROW_NUMBER() OVER (ORDER BY RN), NULL, 'FALSE') as [KURT_P x],
       wct.MovingKURT_P(y, 10, ROW_NUMBER() OVER (ORDER BY RN), 1, 'FALSE') as [KURT_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.

rnxyKURT_P xKURT_P y
1101117NULLNULL
29197NULLNULL
396121NULLNULL
496103-1-1.74865292550528
58674-1.04437869822485-0.902696778864363
69580-0.633226500060853-1.44218560769896
791105-0.650501377317926-1.19882743064317
810272-0.87922814555185-1.44526829938783
994108-0.6351-1.33892142165806
1011094-0.0343734328383585-1.19062985993896
11121850.422858034963324-1.04488822774135
1211590-0.818610577428787-1.01713153752299
1311296-1.26556029586073-1.2906972012198
1410097-1.20386093172745-1.14102818165708
15124106-1.34808680374716-0.85107152806444
169261-1.37425975149999-0.322336560384266
1792107-1.38100802479503-0.367905182528677
1813992-0.8097419156142911.21864906572765
1995101-0.7890174897119341.61602202341904
2090104-0.9702380952380951.48096423857436