# SQL Server Fleiss's Kappa function

KAPPA_FLEISS

Updated: 24 May 2013

Use the scalar function KAPPA_FLEISS to calculate Fleiss’ kappa (Κ) as an index of inter-rater agreement between m raters on categorical data. KAPPA_FLEISS_TV also calculates the category-wise kappa and the corresponding test statistics. The equation for Fleiss’ kappa is

Syntax
SELECT [wctStatistics].[wct].[KAPPA_FLEISS](
<@InputData_RangeQuery, nvarchar(max),>
,<@RV, nvarchar(4000),>
,<@Rating, sql_variant,>)
Arguments
@InputData_RangeQuery
a T-SQL statement, as a string, that specifies the subject, rater, and rating values.
@RV
the value to be returned by the function. Use the following values:

 'K' the kappa value 'SE' the standard error 'Z' the test statistic 'P' the p-value 'KJ' the kappa value for the jth rating 'SEKJ' the standard error for the jth rating 'ZKJ' the test statistic for the jth rating 'PKJ' the p-value for the jth rating
@Rating
the rating for which you want the value returned.
Return Type
Float
Remarks
·         The function is insensitive to order; it does not matter what order the ratings are passed in.
·         Subjects, raters, ratings are automatically combined by the function.
Examples
In this example we calculate the K, SE, Z, as p-values across all ratings
SELECT *
INTO #k
FROM (
SELECT' Subject 01','A',5 UNION ALL
SELECT' Subject 01','B',5 UNION ALL
SELECT' Subject 01','C',5 UNION ALL
SELECT' Subject 01','D',5 UNION ALL
SELECT' Subject 01','E',5 UNION ALL
SELECT' Subject 01','F',5 UNION ALL
SELECT' Subject 01','G',5 UNION ALL
SELECT' Subject 01','H',5 UNION ALL
SELECT' Subject 01','I',5 UNION ALL
SELECT' Subject 01','J',5 UNION ALL
SELECT' Subject 01','K',5 UNION ALL
SELECT' Subject 01','L',5 UNION ALL
SELECT' Subject 01','M',5 UNION ALL
SELECT' Subject 01','N',5 UNION ALL
SELECT' Subject 02','A',2 UNION ALL
SELECT' Subject 02','B',2 UNION ALL
SELECT' Subject 02','C',3 UNION ALL
SELECT' Subject 02','D',3 UNION ALL
SELECT' Subject 02','E',3 UNION ALL
SELECT' Subject 02','F',3 UNION ALL
SELECT' Subject 02','G',3 UNION ALL
SELECT' Subject 02','H',3 UNION ALL
SELECT' Subject 02','I',4 UNION ALL
SELECT' Subject 02','J',4 UNION ALL
SELECT' Subject 02','K',4 UNION ALL
SELECT' Subject 02','L',4 UNION ALL
SELECT' Subject 02','M',5 UNION ALL
SELECT' Subject 02','N',5 UNION ALL
SELECT' Subject 03','A',3 UNION ALL
SELECT' Subject 03','B',3 UNION ALL
SELECT' Subject 03','C',3 UNION ALL
SELECT' Subject 03','D',4 UNION ALL
SELECT' Subject 03','E',4 UNION ALL
SELECT' Subject 03','F',4 UNION ALL
SELECT' Subject 03','G',4 UNION ALL
SELECT' Subject 03','H',4 UNION ALL
SELECT' Subject 03','I',5 UNION ALL
SELECT' Subject 03','J',5 UNION ALL
SELECT' Subject 03','K',5 UNION ALL
SELECT' Subject 03','L',5 UNION ALL
SELECT' Subject 03','M',5 UNION ALL
SELECT' Subject 03','N',5 UNION ALL
SELECT' Subject 04','A',2 UNION ALL
SELECT' Subject 04','B',2 UNION ALL
SELECT' Subject 04','C',2 UNION ALL
SELECT' Subject 04','D',3 UNION ALL
SELECT' Subject 04','E',3 UNION ALL
SELECT' Subject 04','F',3 UNION ALL
SELECT' Subject 04','G',3 UNION ALL
SELECT' Subject 04','H',3 UNION ALL
SELECT' Subject 04','I',3 UNION ALL
SELECT' Subject 04','J',3 UNION ALL
SELECT' Subject 04','K',3 UNION ALL
SELECT' Subject 04','L',3 UNION ALL
SELECT' Subject 04','M',4 UNION ALL
SELECT' Subject 04','N',4 UNION ALL
SELECT' Subject 05','A',1 UNION ALL
SELECT' Subject 05','B',1 UNION ALL
SELECT' Subject 05','C',2 UNION ALL
SELECT' Subject 05','D',2 UNION ALL
SELECT' Subject 05','E',3 UNION ALL
SELECT' Subject 05','F',3 UNION ALL
SELECT' Subject 05','G',3 UNION ALL
SELECT' Subject 05','H',3 UNION ALL
SELECT' Subject 05','I',3 UNION ALL
SELECT' Subject 05','J',3 UNION ALL
SELECT' Subject 05','K',3 UNION ALL
SELECT' Subject 05','L',3 UNION ALL
SELECT' Subject 05','M',4 UNION ALL
SELECT' Subject 05','N',5 UNION ALL
SELECT' Subject 06','A',1 UNION ALL
SELECT' Subject 06','B',1 UNION ALL
SELECT' Subject 06','C',1 UNION ALL
SELECT' Subject 06','D',1 UNION ALL
SELECT' Subject 06','E',1 UNION ALL
SELECT' Subject 06','F',1 UNION ALL
SELECT' Subject 06','G',1 UNION ALL
SELECT' Subject 06','H',2 UNION ALL
SELECT' Subject 06','I',2 UNION ALL
SELECT' Subject 06','J',2 UNION ALL
SELECT' Subject 06','K',2 UNION ALL
SELECT' Subject 06','L',2 UNION ALL
SELECT' Subject 06','M',2 UNION ALL
SELECT' Subject 06','N',2 UNION ALL
SELECT' Subject 07','A',1 UNION ALL
SELECT' Subject 07','B',1 UNION ALL
SELECT' Subject 07','C',1 UNION ALL
SELECT' Subject 07','D',2 UNION ALL
SELECT' Subject 07','E',2 UNION ALL
SELECT' Subject 07','F',3 UNION ALL
SELECT' Subject 07','G',3 UNION ALL
SELECT' Subject 07','H',3 UNION ALL
SELECT' Subject 07','I',3 UNION ALL
SELECT' Subject 07','J',3 UNION ALL
SELECT' Subject 07','K',3 UNION ALL
SELECT' Subject 07','L',4 UNION ALL
SELECT' Subject 07','M',4 UNION ALL
SELECT' Subject 07','N',4 UNION ALL
SELECT' Subject 08','A',1 UNION ALL
SELECT' Subject 08','B',1 UNION ALL
SELECT' Subject 08','C',2 UNION ALL
SELECT' Subject 08','D',2 UNION ALL
SELECT' Subject 08','E',2 UNION ALL
SELECT' Subject 08','F',2 UNION ALL
SELECT' Subject 08','G',2 UNION ALL
SELECT' Subject 08','H',3 UNION ALL
SELECT' Subject 08','I',3 UNION ALL
SELECT' Subject 08','J',3 UNION ALL
SELECT' Subject 08','K',4 UNION ALL
SELECT' Subject 08','L',4 UNION ALL
SELECT' Subject 08','M',5 UNION ALL
SELECT' Subject 08','N',5 UNION ALL
SELECT' Subject 09','A',1 UNION ALL
SELECT' Subject 09','B',1 UNION ALL
SELECT' Subject 09','C',1 UNION ALL
SELECT' Subject 09','D',1 UNION ALL
SELECT' Subject 09','E',1 UNION ALL
SELECT' Subject 09','F',1 UNION ALL
SELECT' Subject 09','G',2 UNION ALL
SELECT' Subject 09','H',2 UNION ALL
SELECT' Subject 09','I',2 UNION ALL
SELECT' Subject 09','J',2 UNION ALL
SELECT' Subject 09','K',2 UNION ALL
SELECT' Subject 09','L',3 UNION ALL
SELECT' Subject 09','M',3 UNION ALL
SELECT' Subject 09','N',4 UNION ALL
SELECT' Subject 10','A',2 UNION ALL
SELECT' Subject 10','B',2 UNION ALL
SELECT' Subject 10','C',3 UNION ALL
SELECT' Subject 10','D',3 UNION ALL
SELECT' Subject 10','E',4 UNION ALL
SELECT' Subject 10','F',4 UNION ALL
SELECT' Subject 10','G',4 UNION ALL
SELECT' Subject 10','H',5 UNION ALL
SELECT' Subject 10','I',5 UNION ALL
SELECT' Subject 10','J',5 UNION ALL
SELECT' Subject 10','K',5 UNION ALL
SELECT' Subject 10','L',5 UNION ALL
SELECT' Subject 10','M',5 UNION ALL
SELECT' Subject 10','N',5
)n(subject, rater, rating)

SELECT p.stat
,wct.KAPPA_FLEISS('SELECT subject, rater, rating FROM #k', p.stat, NULL) as stat_value
FROM (
SELECT 'K' UNION ALL
SELECT 'SE' UNION ALL
SELECT 'P' UNION ALL
SELECT 'Z'
)p(stat)
DROP TABLE #k
This produces the following result.

In this example we calculate the K, Z, as p-values for each rating.
SELECT *
INTO #k
FROM (
SELECT' Subject 01','A',5 UNION ALL
SELECT' Subject 01','B',5 UNION ALL
SELECT' Subject 01','C',5 UNION ALL
SELECT' Subject 01','D',5 UNION ALL
SELECT' Subject 01','E',5 UNION ALL
SELECT' Subject 01','F',5 UNION ALL
SELECT' Subject 01','G',5 UNION ALL
SELECT' Subject 01','H',5 UNION ALL
SELECT' Subject 01','I',5 UNION ALL
SELECT' Subject 01','J',5 UNION ALL
SELECT' Subject 01','K',5 UNION ALL
SELECT' Subject 01','L',5 UNION ALL
SELECT' Subject 01','M',5 UNION ALL
SELECT' Subject 01','N',5 UNION ALL
SELECT' Subject 02','A',2 UNION ALL
SELECT' Subject 02','B',2 UNION ALL
SELECT' Subject 02','C',3 UNION ALL
SELECT' Subject 02','D',3 UNION ALL
SELECT' Subject 02','E',3 UNION ALL
SELECT' Subject 02','F',3 UNION ALL
SELECT' Subject 02','G',3 UNION ALL
SELECT' Subject 02','H',3 UNION ALL
SELECT' Subject 02','I',4 UNION ALL
SELECT' Subject 02','J',4 UNION ALL
SELECT' Subject 02','K',4 UNION ALL
SELECT' Subject 02','L',4 UNION ALL
SELECT' Subject 02','M',5 UNION ALL
SELECT' Subject 02','N',5 UNION ALL
SELECT' Subject 03','A',3 UNION ALL
SELECT' Subject 03','B',3 UNION ALL
SELECT' Subject 03','C',3 UNION ALL
SELECT' Subject 03','D',4 UNION ALL
SELECT' Subject 03','E',4 UNION ALL
SELECT' Subject 03','F',4 UNION ALL
SELECT' Subject 03','G',4 UNION ALL
SELECT' Subject 03','H',4 UNION ALL
SELECT' Subject 03','I',5 UNION ALL
SELECT' Subject 03','J',5 UNION ALL
SELECT' Subject 03','K',5 UNION ALL
SELECT' Subject 03','L',5 UNION ALL
SELECT' Subject 03','M',5 UNION ALL
SELECT' Subject 03','N',5 UNION ALL
SELECT' Subject 04','A',2 UNION ALL
SELECT' Subject 04','B',2 UNION ALL
SELECT' Subject 04','C',2 UNION ALL
SELECT' Subject 04','D',3 UNION ALL
SELECT' Subject 04','E',3 UNION ALL
SELECT' Subject 04','F',3 UNION ALL
SELECT' Subject 04','G',3 UNION ALL
SELECT' Subject 04','H',3 UNION ALL
SELECT' Subject 04','I',3 UNION ALL
SELECT' Subject 04','J',3 UNION ALL
SELECT' Subject 04','K',3 UNION ALL
SELECT' Subject 04','L',3 UNION ALL
SELECT' Subject 04','M',4 UNION ALL
SELECT' Subject 04','N',4 UNION ALL
SELECT' Subject 05','A',1 UNION ALL
SELECT' Subject 05','B',1 UNION ALL
SELECT' Subject 05','C',2 UNION ALL
SELECT' Subject 05','D',2 UNION ALL
SELECT' Subject 05','E',3 UNION ALL
SELECT' Subject 05','F',3 UNION ALL
SELECT' Subject 05','G',3 UNION ALL
SELECT' Subject 05','H',3 UNION ALL
SELECT' Subject 05','I',3 UNION ALL
SELECT' Subject 05','J',3 UNION ALL
SELECT' Subject 05','K',3 UNION ALL
SELECT' Subject 05','L',3 UNION ALL
SELECT' Subject 05','M',4 UNION ALL
SELECT' Subject 05','N',5 UNION ALL
SELECT' Subject 06','A',1 UNION ALL
SELECT' Subject 06','B',1 UNION ALL
SELECT' Subject 06','C',1 UNION ALL
SELECT' Subject 06','D',1 UNION ALL
SELECT' Subject 06','E',1 UNION ALL
SELECT' Subject 06','F',1 UNION ALL
SELECT' Subject 06','G',1 UNION ALL
SELECT' Subject 06','H',2 UNION ALL
SELECT' Subject 06','I',2 UNION ALL
SELECT' Subject 06','J',2 UNION ALL
SELECT' Subject 06','K',2 UNION ALL
SELECT' Subject 06','L',2 UNION ALL
SELECT' Subject 06','M',2 UNION ALL
SELECT' Subject 06','N',2 UNION ALL
SELECT' Subject 07','A',1 UNION ALL
SELECT' Subject 07','B',1 UNION ALL
SELECT' Subject 07','C',1 UNION ALL
SELECT' Subject 07','D',2 UNION ALL
SELECT' Subject 07','E',2 UNION ALL
SELECT' Subject 07','F',3 UNION ALL
SELECT' Subject 07','G',3 UNION ALL
SELECT' Subject 07','H',3 UNION ALL
SELECT' Subject 07','I',3 UNION ALL
SELECT' Subject 07','J',3 UNION ALL
SELECT' Subject 07','K',3 UNION ALL
SELECT' Subject 07','L',4 UNION ALL
SELECT' Subject 07','M',4 UNION ALL
SELECT' Subject 07','N',4 UNION ALL
SELECT' Subject 08','A',1 UNION ALL
SELECT' Subject 08','B',1 UNION ALL
SELECT' Subject 08','C',2 UNION ALL
SELECT' Subject 08','D',2 UNION ALL
SELECT' Subject 08','E',2 UNION ALL
SELECT' Subject 08','F',2 UNION ALL
SELECT' Subject 08','G',2 UNION ALL
SELECT' Subject 08','H',3 UNION ALL
SELECT' Subject 08','I',3 UNION ALL
SELECT' Subject 08','J',3 UNION ALL
SELECT' Subject 08','K',4 UNION ALL
SELECT' Subject 08','L',4 UNION ALL
SELECT' Subject 08','M',5 UNION ALL
SELECT' Subject 08','N',5 UNION ALL
SELECT' Subject 09','A',1 UNION ALL
SELECT' Subject 09','B',1 UNION ALL
SELECT' Subject 09','C',1 UNION ALL
SELECT' Subject 09','D',1 UNION ALL
SELECT' Subject 09','E',1 UNION ALL
SELECT' Subject 09','F',1 UNION ALL
SELECT' Subject 09','G',2 UNION ALL
SELECT' Subject 09','H',2 UNION ALL
SELECT' Subject 09','I',2 UNION ALL
SELECT' Subject 09','J',2 UNION ALL
SELECT' Subject 09','K',2 UNION ALL
SELECT' Subject 09','L',3 UNION ALL
SELECT' Subject 09','M',3 UNION ALL
SELECT' Subject 09','N',4 UNION ALL
SELECT' Subject 10','A',2 UNION ALL
SELECT' Subject 10','B',2 UNION ALL
SELECT' Subject 10','C',3 UNION ALL
SELECT' Subject 10','D',3 UNION ALL
SELECT' Subject 10','E',4 UNION ALL
SELECT' Subject 10','F',4 UNION ALL
SELECT' Subject 10','G',4 UNION ALL
SELECT' Subject 10','H',5 UNION ALL
SELECT' Subject 10','I',5 UNION ALL
SELECT' Subject 10','J',5 UNION ALL
SELECT' Subject 10','K',5 UNION ALL
SELECT' Subject 10','L',5 UNION ALL
SELECT' Subject 10','M',5 UNION ALL
SELECT' Subject 10','N',5
)n(subject, rater, rating)

SELECT r.rating
,p.stat
,wct.KAPPA_FLEISS('SELECT subject, rater, rating FROM #k', p.stat, r.rating) as stat_value
FROM (
SELECT 'KJ' UNION ALL
SELECT 'PKJ' UNION ALL
SELECT 'ZKJ'
)p(stat)
CROSS APPLY(SELECT DISTINCT rating FROM #k)r(rating)
ORDER BY 2, 1

DROP TABLE #k
This produces the following result.