2012-06-20 182 views
0

所以,如果我有一個表如下SQL查找重複每用戶記錄

與列

date  datetime 
employee varchar 

,我想找到的所有記錄,其中一個特定的用戶是相同的日期不止一次

併爲每個重複的發現,我需要

date employee numDuplicates 

回答

3

您可以使用GROUP BYHAVING條款:

SELECT employee, [date], Count(*) As numDuplicates 
FROM Table 
GROUP BY employee, [date] 
HAVING COUNT(*) > 1 
ORDER BY COUNT(*) DESC 
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