SQL Server:检索列中的重复值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1518635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:52:54  来源:igfitidea点击:

SQL Server: Retrieve the duplicate value in a column

sqlsql-server

提问by Ricky

How could I filter out the duplicate value in a column with SQL syntax?

如何使用 SQL 语法过滤掉列中的重复值?

Thanks.

谢谢。

回答by Mead

A common question, though filtering out suggests you want to ignore the duplicate ones? If so, listing unique values:

一个常见的问题,尽管过滤掉表明您想忽略重复的?如果是这样,请列出唯一值:

SELECT col 
FROM table
GROUP BY col 
HAVING (COUNT(col) =1 )

If you just want to filter so you are left withthe duplicates

如果你只是想过滤所以你会留下重复的

SELECT col, COUNT(col) AS dup_count
FROM table
GROUP BY col
HAVING (COUNT(col) > 1) 

Used www.mximize.com/how-to-find-duplicate-values-in-a-table- as a base in 2009; website content has now gone (2014).

2009 年使用 www.mximize.com/how-to-find-duplicate-values-in-a-table- 作为基础;网站内容现已消失(2014 年)。

回答by adatapost

Use DISTINCTor GROUP BY** clause.

使用DISTINCT或 GROUP BY** 子句。

Select DISTINCT City from TableName

OR

Select City from TableName GROUP BY City

回答by Filip Ekberg

Depending on how you mean "filter" you could either use DISTINCTor maybe GROUPBY both are used to Remove or Group duplicate entries.

根据您的意思“过滤器”,您可以使用DISTINCTGROUPBY 两者都用于删除或分组重复条目。

Check the links for more information.

检查链接以获取更多信息。

A snippet from the DISTINCT-link above:

上面 DISTINCT 链接中的一个片段:

SELECT DISTINCT od.productid
FROM [order details] OD

SELECT od.productid
FROM [order details] OD
GROUP BY od.productid

Both of these generally result in the same output.

这两者通常会产生相同的输出。

回答by James Ball

You askedfor a list of the duplicates. Here is a simple way using the except operator (SQL 2008 +).

要求提供重复项列表。这是使用except运算符的简单方法(SQL 2008+)。

  select [column] from Table1 

    except

  select distinct [column] from Table1; 

Alternatively, you could use standard SQL

或者,您可以使用标准 SQL

select [column] from Table1 

where [column} not in 

     (select distinct [column} from Table1);

回答by Nils

distinctwould be the keyword to filter douplicates. May be you can explain a little more what you're trying to achieve ?

distinct是过滤重复项的关键字。也许你可以解释一下你想要实现的目标?