Question
If I have data like this:
+---+----+
|Key|Name|
+---+----+
|1 |Dan |
+---+----+
|2 |Tom |
+---+----+
|3 |Jon |
+---+----+
|4 |Tom |
+---+----+
|5 |Sam |
+---+----+
|6 |Dan |
+---+----+
What is the SQL query to bring back the records where Name is repeated 2 or more times?
So the result I would want is
+---+
|Tom|
+---+
|Dan|
+---+
Answer
Couldn't be simpler...
Select
Name,
Count(Name) As Count
From
Table
Group By
Name
Having
Count(Name) > 1
Order By
Count(Name) Desc
This could also be extended to delete duplicates:
Delete
From
Table
Where
Key In (
Select
Max(Key)
From
Table
Group By
Name
Having
Count(Name) > 1)
< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/3196/" >SQL query, count and group by< /a>
0 comments:
Post a Comment