I would think that you should be able to unpivot and count the occurrences in ways that your business rules require. Based on my (limited) understanding of your rules, it can, for example, be done like shown below.If having to manually list the key columns is not an acceptable solution, the only other thought that comes to mind is to write a dynamic query. However, I am a reluctant user of dynamic SQL - so I will defer that to someone else.CREATE TABLE #tbl(TestId INT, keya CHAR(3), keyb CHAR(3), keyc char(3), keyd CHAR(3))INSERT INTO #tbl VALUES ('1','111','222','333','444'),('2','555','666','777','888'),('3','555','000','000','222'),('4','111','333','444','888'),('5','111','222','111','333')SELECT DISTINCT kys,testidFROM ( SELECT testId, kys, COUNT(*) OVER (PARTITION BY testid,kys) N1, COUNT(*) OVER (PARTITION BY kys) N2 FROM #tbl UNPIVOT (kys FOR k IN (keya, keyb, keyc, keyd))U) sWHERE N1 < N2 ORDER BY 1,2DROP TABLE #tbl
________________________________________-- Yes, I am indeed a fictional character.