mjordan
mjordan

Reputation: 369

SQL Server find Unique rows on multiple columns

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.

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

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

Upvotes: 0

Views: 44

Answers (1)

ShaneCoder
ShaneCoder

Reputation: 781

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

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

Upvotes: 4

Related Questions