MovingKURT_S
Updated 2023-11-13 21:27:53.697000
Syntax
SELECT [westclintech].[wct].[MovingKURT_S](
<@Val, float,>
,<@Offset, int,>
,<@RowNum, int,>
,<@Id, tinyint,>
,<@Exact, bit,>)
Description
Use the scalar function MovingKURT_S to calculate the sample kurtosis of column values in an ordered resultant table, without the need for a self-join. The sample 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.
Arguments
@Id
a unique identifier for the MovingKURT_S calculation. @Id allows you to specify multiple moving sample kurtosis calculations within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.
@Offset
specifies the window size. @Offset is an expression of type int or of a type that can be implicitly converted to int.
@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.
@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.
@RowNum
the number of the row within the group for which the sample 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.
Return Type
float
Remarks
If @Id is NULL then @Id = 0.
To calculate the sample kurtosis from the beginning of a dataset or a partition, use the RunningKURT_S function.
To calculate the sample kurtosis for an entire data set or for an entire group within a data set use the KURTOSIS_S function.
If @RowNum is equal to 1, MovingKURT_S 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 sample kurtosis of x and y over a window of 10 rows. Note that the @Id value for each MovingKURT_S column is different. Since @Exact is NULL, NULL is returned when the window size is less than @Offset .
SELECT rn,
x,
y,
wct.MovingKURT_S(x, 10, ROW_NUMBER() OVER (ORDER BY RN), NULL, NULL) as [KURT_S x],
wct.MovingKURT_S(y, 10, ROW_NUMBER() OVER (ORDER BY RN), 1, NULL) as [KURT_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":"KURT_S x"},{"field":"KURT_S y"}],"rows":[{"rn":"1","x":"101","y":"117","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"2","x":"91","y":"97","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"3","x":"96","y":"121","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"4","x":"96","y":"103","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"5","x":"86","y":"74","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"6","x":"95","y":"80","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"7","x":"91","y":"105","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"8","x":"102","y":"72","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"9","x":"94","y":"108","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"10","x":"110","y":"94","KURT_S x":"0.903518395517902","KURT_S y":"-1.14057778810637"},{"rn":"11","x":"121","y":"85","KURT_S x":"1.71183831181016","KURT_S y":"-0.882927402614166"},{"rn":"12","x":"115","y":"90","KURT_S x":"-0.482900842240177","KURT_S y":"-0.833857539549565"},{"rn":"13","x":"112","y":"96","KURT_S x":"-1.27304409446808","KURT_S y":"-1.31748255215644"},{"rn":"14","x":"100","y":"97","KURT_S x":"-1.16396843287532","KURT_S y":"-1.05288910685806"},{"rn":"15","x":"124","y":"106","KURT_S x":"-1.41893917091015","KURT_S y":"-0.54028716568535"},{"rn":"16","x":"92","y":"61","KURT_S x":"-1.46520920354463","KURT_S y":"0.394440723606388"},{"rn":"17","x":"92","y":"107","KURT_S x":"-1.47713918669122","KURT_S y":"0.313881909458232"},{"rn":"18","x":"139","y":"92","KURT_S x":"-0.467222315103836","KURT_S y":"3.11868316976853"},{"rn":"19","x":"95","y":"101","KURT_S x":"-0.430584490740741","KURT_S y":"3.82118179140153"},{"rn":"20","x":"90","y":"104","KURT_S x":"-0.750956632653061","KURT_S y":"3.58241892176539"}]}
This example uses the same data as the previous example, however @Exact has been set to 'FALSE' .
SELECT rn,
x,
y,
wct.MovingKURT_S(x, 10, ROW_NUMBER() OVER (ORDER BY RN), NULL, 'FALSE') as [KURT_S x],
wct.MovingKURT_S(y, 10, ROW_NUMBER() OVER (ORDER BY RN), 1, 'FALSE') as [KURT_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":"KURT_S x"},{"field":"KURT_S y"}],"rows":[{"rn":"1","x":"101","y":"117","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"2","x":"91","y":"97","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"3","x":"96","y":"121","KURT_S x":"NULL","KURT_S y":"NULL"},{"rn":"4","x":"96","y":"103","KURT_S x":"1.5","KURT_S y":"-4.11489694128959"},{"rn":"5","x":"86","y":"74","KURT_S x":"-0.177514792899409","KURT_S y":"0.38921288454255"},{"rn":"6","x":"95","y":"80","KURT_S x":"0.653089374822511","KURT_S y":"-1.70637468912197"},{"rn":"7","x":"91","y":"105","KURT_S x":"0.238796694436977","KURT_S y":"-1.07718583354361"},{"rn":"8","x":"102","y":"72","KURT_S x":"-0.446379105658885","KURT_S y":"-1.63506342871445"},{"rn":"9","x":"94","y":"108","KURT_S x":"-0.0668571428571427","KURT_S y":"-1.40746937458679"},{"rn":"10","x":"110","y":"94","KURT_S x":"0.903518395517902","KURT_S y":"-1.14057778810637"},{"rn":"11","x":"121","y":"85","KURT_S x":"1.71183831181016","KURT_S y":"-0.882927402614166"},{"rn":"12","x":"115","y":"90","KURT_S x":"-0.482900842240177","KURT_S y":"-0.833857539549565"},{"rn":"13","x":"112","y":"96","KURT_S x":"-1.27304409446808","KURT_S y":"-1.31748255215644"},{"rn":"14","x":"100","y":"97","KURT_S x":"-1.16396843287532","KURT_S y":"-1.05288910685806"},{"rn":"15","x":"124","y":"106","KURT_S x":"-1.41893917091015","KURT_S y":"-0.54028716568535"},{"rn":"16","x":"92","y":"61","KURT_S x":"-1.46520920354463","KURT_S y":"0.394440723606388"},{"rn":"17","x":"92","y":"107","KURT_S x":"-1.47713918669122","KURT_S y":"0.313881909458232"},{"rn":"18","x":"139","y":"92","KURT_S x":"-0.467222315103836","KURT_S y":"3.11868316976853"},{"rn":"19","x":"95","y":"101","KURT_S x":"-0.430584490740741","KURT_S y":"3.82118179140153"},{"rn":"20","x":"90","y":"104","KURT_S x":"-0.750956632653061","KURT_S y":"3.58241892176539"}]}