所以如果我有一张如下表
带柱
date datetime
employee varchar
我想查找特定用户多次具有相同日期的所有记录
对于找到的每个重复项,我都需要
date employee numDuplicates
所以如果我有一张如下表
带柱
date datetime
employee varchar
我想查找特定用户多次具有相同日期的所有记录
对于找到的每个重复项,我都需要
date employee numDuplicates
您可以使用GROUP BY
withHAVING
子句:
SELECT employee, [date], Count(*) As numDuplicates
FROM Table
GROUP BY employee, [date]
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
如果您在日期时间字段中存储日期和时间,则需要提取日期以进行分组。在 SQL Server 2008 及更高版本中,您可以执行以下操作:
select employee, cast([date] as date), count(*) as numDuplicates
from t
group by employee, cast([date] as date)
having count(*) > 1