Kevin
Kevin

Reputation: 77

SQL Server: Return only unique values per group(not distinct)(not one of each value)

I need to return one unique value per group of values in one column, like the below example

    COL 1   | COL 2 
    1   |A  
    1   |A  
    2   |B
    2   |C
    2   |C
    3   |A
    3   |B
    3   |D
    3   |D

Return:

    COL 1   | COL 2 
    2   |B
    3   |A
    3   |B

Upvotes: 1

Views: 247

Answers (1)

Rossana
Rossana

Reputation: 187

Use an aggregate function COUNT() so you can filter unique row.

SELECT Col1, Col2
FROM yourtable
GROUP BY Col1, Col2
HAVING COUNT(*) = 1

Upvotes: 3

Related Questions