Logo

SQL Server MovingSKEW_P Function

Updated 2023-11-13 21:40:38.277000

Description

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

@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 skewnesss from the beginning of a dataset or a partition, use the RunningSKEW_P function.

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

If @RowNum is equal to 1, MovingSKEW_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 skewness of x and y over a window of 10 rows. Note that the @Id value for each MovingSKEW_P column is different. Since @Exact is NULL, NULL is returned when the window size is less than @Offset .

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

rnxySKEW_P xSKEW_P y
1101117NULLNULL
29197NULLNULL
396121NULLNULL
496103NULLNULL
58674NULLNULL
69580NULLNULL
791105NULLNULL
810272NULLNULL
994108NULLNULL
10110940.59794064967205-0.223688906272517
11121851.148087637399850.0885382177825925
12115900.6318438708452520.224579774862171
13112960.247845492717323-0.137991148973669
14100970.166326319556557-0.103320897541807
151241060.150533587918778-0.411698835729833
1692610.114791992347527-0.841177595353945
17921070.13688246083236-0.807116763237067
18139920.385280830439978-1.25087766239486
19951010.395585033305956-1.41059257665943
20901040.493903575245788-1.44004210527361

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

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

rnxySKEW_P xSKEW_P y
1101117NULLNULL
29197NULLNULL
3961210-0.630903856710625
4961030-0.0748700991297026
58674-0.271545417883639-0.579957911668973
69580-0.400108877081824-0.131152422955013
791105-0.1387284841486-0.296501322167989
810272-0.146800156380065-0.0797409447174199
994108-0.102530483272052-0.273202158161578
10110940.59794064967205-0.223688906272517
11121851.148087637399850.0885382177825925
12115900.6318438708452520.224579774862171
13112960.247845492717323-0.137991148973669
14100970.166326319556557-0.103320897541807
151241060.150533587918778-0.411698835729833
1692610.114791992347527-0.841177595353945
17921070.13688246083236-0.807116763237067
18139920.385280830439978-1.25087766239486
19951010.395585033305956-1.41059257665943
20901040.493903575245788-1.44004210527361