SQL查询,计数和分组

前端之家收集整理的这篇文章主要介绍了SQL查询,计数和分组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我有这样的数据:
+---+----+
|Key|Name|
+---+----+
|1  |Dan |
+---+----+
|2  |Tom |
+---+----+
|3  |Jon |
+---+----+
|4  |Tom |
+---+----+
|5  |Sam |
+---+----+
|6  |Dan |
+---+----+

什么是SQL查询带回的名称重复2次以上的记录?

所以我想要的结果是

+---+
 |Tom|
 +---+
 |Dan|
 +---+

解决方法

不能简单…
Select
Name,Count(Name) As Count
From
Table
Group By
Name
Having
Count(Name) > 1
Order By
Count(Name) Desc

这也可以扩展到删除重复项:

Delete
From
Table
Where
Key In (
    Select
    Max(Key)
    From
    Table
    Group By
    Name
    Having
Count(Name) > 1)
原文链接:https://www.f2er.com/mssql/83167.html

猜你在找的MsSQL相关文章