# SQL Server McNemar's chi-squared test

MCNEMAR

Updated: 24 May 2013

Use the scalar function MCNEMAR to perform McNemar’s chi-squared test for symmetry of rows and columns in a two-dimensional contingency table.
Syntax
SELECT [wctStatistics].[wct].[MCNEMAR](
<@InputData_RangeQuery, nvarchar(max),>
,<@Correct, bit,>
,<@RV, nvarchar(4000),>)
Arguments
@InputData_RangeQuery
a T-SQL statement, as a string, that specifies the test 1- and test 2-values.
@Correct
a bit value identifying whether or not to apply continuity correction when computing the test statistic.
@RV
the value to be returned by the function. Use the following values:

 'P' the p-value 'X' the test statistic
Return Type
float
Remarks
·         The function is insensitive to order; it does not matter what order the test 1- and test 2-values are passed in.
·         Test 1 and test 2 values are passed in as pairs.
·         MCNEMAR only works with 2 test results.
·         For more than 2 test results, consider using KAPPA_COHEN or KAPPA_FLEISS.
Examples
SELECT *
INTO #m
FROM (
SELECT 1,'B','A' UNION ALL
SELECT 2,'B','B' UNION ALL
SELECT 3,'A','A' UNION ALL
SELECT 4,'B','A' UNION ALL
SELECT 5,'A','A' UNION ALL
SELECT 6,'A','A' UNION ALL
SELECT 7,'B','A' UNION ALL
SELECT 8,'B','A' UNION ALL
SELECT 9,'A','B' UNION ALL
SELECT 10,'B','A' UNION ALL
SELECT 11,'B','B' UNION ALL
SELECT 12,'A','A' UNION ALL
SELECT 13,'A','B' UNION ALL
SELECT 14,'A','A' UNION ALL
SELECT 15,'B','B' UNION ALL
SELECT 16,'A','A' UNION ALL
SELECT 17,'A','A' UNION ALL
SELECT 18,'B','B' UNION ALL
SELECT 19,'B','A' UNION ALL
SELECT 20,'B','A' UNION ALL
SELECT 21,'B','A' UNION ALL
SELECT 22,'B','A' UNION ALL
SELECT 23,'A','A' UNION ALL
SELECT 24,'B','A' UNION ALL
SELECT 25,'A','A' UNION ALL
SELECT 26,'A','A' UNION ALL
SELECT 27,'B','A' UNION ALL
SELECT 28,'B','B' UNION ALL
SELECT 29,'A','B' UNION ALL
SELECT 30,'B','B' UNION ALL
SELECT 31,'B','B' UNION ALL
SELECT 32,'B','A' UNION ALL
SELECT 33,'A','B' UNION ALL
SELECT 34,'A','B' UNION ALL
SELECT 35,'B','A' UNION ALL
SELECT 36,'B','A' UNION ALL
SELECT 37,'A','A' UNION ALL
SELECT 38,'A','A' UNION ALL
SELECT 39,'B','B' UNION ALL
SELECT 40,'A','A' UNION ALL
SELECT 41,'B','A' UNION ALL
SELECT 42,'A','B' UNION ALL
SELECT 43,'B','B' UNION ALL
SELECT 44,'B','A' UNION ALL
SELECT 45,'B','B' UNION ALL
SELECT 46,'A','B' UNION ALL
SELECT 47,'A','A' UNION ALL
SELECT 48,'A','A' UNION ALL
SELECT 49,'B','B' UNION ALL
SELECT 50,'B','B' UNION ALL
SELECT 51,'B','A' UNION ALL
SELECT 52,'A','B' UNION ALL
SELECT 53,'A','A' UNION ALL
SELECT 54,'A','B' UNION ALL
SELECT 55,'A','B' UNION ALL
SELECT 56,'B','A' UNION ALL
SELECT 57,'B','B' UNION ALL
SELECT 58,'A','A' UNION ALL
SELECT 59,'B','B' UNION ALL
SELECT 60,'B','A' UNION ALL
SELECT 61,'B','A' UNION ALL
SELECT 62,'A','B' UNION ALL
SELECT 63,'B','B' UNION ALL
SELECT 64,'B','A' UNION ALL
SELECT 65,'B','B' UNION ALL
SELECT 66,'B','A' UNION ALL
SELECT 67,'B','A' UNION ALL
SELECT 68,'B','A' UNION ALL
SELECT 69,'B','A' UNION ALL
SELECT 70,'B','A' UNION ALL
SELECT 71,'B','A' UNION ALL
SELECT 72,'B','A' UNION ALL
SELECT 73,'B','B' UNION ALL
SELECT 74,'A','A' UNION ALL
SELECT 75,'B','A' UNION ALL
SELECT 76,'B','B' UNION ALL
SELECT 77,'A','A' UNION ALL
SELECT 78,'A','B' UNION ALL
SELECT 79,'B','A' UNION ALL
SELECT 80,'A','A' UNION ALL
SELECT 81,'B','A' UNION ALL
SELECT 82,'B','A' UNION ALL
SELECT 83,'A','B' UNION ALL
SELECT 84,'B','B' UNION ALL
SELECT 85,'A','B' UNION ALL
SELECT 86,'A','A' UNION ALL
SELECT 87,'A','B' UNION ALL
SELECT 88,'B','A' UNION ALL
SELECT 89,'B','B' UNION ALL
SELECT 90,'A','B' UNION ALL
SELECT 91,'A','A' UNION ALL
SELECT 92,'B','B' UNION ALL
SELECT 93,'A','B' UNION ALL
SELECT 94,'A','A' UNION ALL
SELECT 95,'B','A' UNION ALL
SELECT 96,'A','A' UNION ALL
SELECT 97,'B','B' UNION ALL
SELECT 98,'B','B' UNION ALL
SELECT 99,'B','A' UNION ALL
SELECT 100,'A','B'
)n(rn,x,y)

SELECT p.stat
,wct.MCNEMAR('SELECT x,y FROM #m', 'True', p.stat) as stat_value
FROM (
SELECT 'X' UNION ALL
SELECT 'P'
)p(stat)

DROP TABLE #m

This produces the following result.