# SQL Server Kendall's W function

KENDALLW_TV

Updated: 24 May 2013

Use the table-valued function KENDALLW_TV to calculate Kendall’s coefficient of concordance (w) as an index of inter-rater reliability for ordinal data. The equation for Kendall’s w is:

Where
r is the sum of the ranks of the ratings for all raters for each subject
µ2 is the second central moment
nr is the number of raters
ns is the number of subjects being rated

The equation for Kendall’s w corrected for ties is:

Where

ti is a count of each rating within a rater
gj is the number of unique ratings within a rater
Syntax
SELECT * FROM [wctStatistics].[wct].[KENDALLW_TV](
<@InputData_RangeQuery, nvarchar(max),>
,<@CorrectTies, bit,>)
Arguments
@InputData_RangeQuery
a T-SQL statement, as a string, that specifies the subject, rater, and rating values.
@CorrectTies
a bit value identifying whether the coefficient should be corrected for ties within raters.
Return Type
RETURNS TABLE (
[W] [float] NULL,
[X] [float] NULL,
[DF1] [float] NULL,
[P] [float] NULL
)

 Column Column Description W the Kendall w statistic X the chi-squared statistic DF1 the degrees of freedom P the p-value
Remarks
·         The function is insensitive to order and automatically matches all the ratings for a subject
·         NULL values are excluded
Examples
SELECT n.s
,x.rater
,x.rating
INTO #k
FROM (
SELECT 1,3,3,2 UNION ALL
SELECT 2,3,6,1 UNION ALL
SELECT 3,3,4,4 UNION ALL
SELECT 4,4,6,4 UNION ALL
SELECT 5,5,2,3 UNION ALL
SELECT 6,5,4,2 UNION ALL
SELECT 7,2,2,1 UNION ALL
SELECT 8,3,4,6 UNION ALL
SELECT 9,5,3,1 UNION ALL
SELECT 10,2,3,1 UNION ALL
SELECT 11,2,2,1 UNION ALL
SELECT 12,6,3,2 UNION ALL
SELECT 13,1,3,3 UNION ALL
SELECT 14,5,3,3 UNION ALL
SELECT 15,2,2,1 UNION ALL
SELECT 16,2,2,1 UNION ALL
SELECT 17,1,1,3 UNION ALL
SELECT 18,2,3,3 UNION ALL
SELECT 19,4,3,2 UNION ALL
SELECT 20,3,4,2
)n(s,r1,r2,r3)

--This CROSS APPLY UNPIVOTS the input data
--into third normal form
CROSS APPLY(
SELECT 'r1', r1 UNION ALL
SELECT 'r2', r2 UNION ALL
SELECT 'r3', r3
)x(rater,rating)

SELECT p.*
FROM wct.KENDALLW_TV('SELECT s,rater,rating FROM #k', 'False')k
--This CROSS APPLY UNPIVOTS the tvf columns for formatting
CROSS APPLY(
SELECT 'W', k.W UNION ALL
SELECT 'X', k.X UNION ALL
SELECT 'df1', k.df1 UNION ALL
SELECT 'p', k.
) p(stat, value_stat)

DROP TABLE #k
This produces the following result.