SQL Server:每组只返回唯一值(不相同)(不是每个值中的一个)

时间:2021-03-10 12:49:06

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

1 个解决方案

#1


3  

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

使用聚合函数COUNT(),以便过滤唯一行。

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

#1


3  

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

使用聚合函数COUNT(),以便过滤唯一行。

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