SQL查找每个用户的重复记录

时间:2012-06-20 21:49:32

标签: sql sql-server

所以如果我有一张表如下

date       datetime
employee   varchar

我希望找到特定用户多次具有相同日期的所有记录

并且每发现一次重复我需要

date    employee    numDuplicates

2 个答案:

答案 0 :(得分:3)

您可以将GROUP BYHAVING子句一起使用:

SELECT employee, [date], Count(*) As numDuplicates
FROM Table
GROUP BY employee, [date]
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

答案 1 :(得分:1)

如果您在日期时间字段中存储日期和时间,则需要提取日期以进行分组。在SQL Server 2008及更高版本中,您可以执行以下操作:

select employee, cast([date] as date), count(*) as numDuplicates
from t
group by employee, cast([date] as date)
having count(*) > 1