SQL Server在多个列上查找唯一行

时间:2021-12-04 16:06:07

I have two columns in one table which when combined should be unique. I need a query to discover which rows do not have a unique combination. Concat([Category], [SEL]) is what needs to be unique, neither are unique on their own.

我在一个表中有两列,组合时应该是唯一的。我需要一个查询来发现哪些行没有唯一的组合。 Concat([Category],[SEL])是必须独特的,它们本身都不是唯一的。

I found the following SELECT, but I don't know how to adapt it for multiple columns.

我找到了以下SELECT,但我不知道如何为多列调整它。

SELECT [Category], [SEL] 
FROM [myTable] 
WHERE [Category] IN (SELECT [Category] 
                     FROM [myTable] 
                     GROUP BY [Category] 
                     HAVING COUNT(*) > 1)

1 个解决方案

#1


4  

Group by both columns. The results will be combinations of Category and SEL that appear in more than one record.

按两列分组。结果将是出现在多个记录中的Category和SEL的组合。

SELECT [Category], [SEL] FROM [myTable] GROUP BY [Category], [SEL] HAVING COUNT(*) > 1

#1


4  

Group by both columns. The results will be combinations of Category and SEL that appear in more than one record.

按两列分组。结果将是出现在多个记录中的Category和SEL的组合。

SELECT [Category], [SEL] FROM [myTable] GROUP BY [Category], [SEL] HAVING COUNT(*) > 1