确定SQL Server表中重复记录的最佳方法是什么?
例如,我想查找表格中收到的最后一封重复的电子邮件(表格包含主键,收到的日期和电子邮件字段)。
示例数据:
1 01/01/2008 stuff@stuff.com
2 02/01/2008 stuff@stuff.com
3 01/12/2008 noone@stuff.com
答案 0 :(得分:9)
类似这样的事情
select email ,max(receiveddate) as MaxDate
from YourTable
group by email
having count(email) > 1
答案 1 :(得分:1)
尝试类似:
SELECT * FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY ReceivedDate, Email ORDER BY ReceivedDate, Email DESC) AS RowNumber
FROM EmailTable
) a
WHERE RowNumber = 1
请参阅http://www.technicaloverload.com/working-with-duplicates-in-sql-server/
答案 2 :(得分:0)
您是否无法加入电子邮件字段中的列表,然后查看结果中的空值?
或者更好的是,计算每个电子邮件地址的实例?并且只返回带有计数的> 1
甚至可以使用电子邮件和ID字段。并返回电子邮件相同的条目,ID不同。 (为避免重复,请不要使用!=而是使用<或&gt ;.)
答案 3 :(得分:0)
试试这个
select * from table a, table b
where a.email = b.email
答案 4 :(得分:0)
SELECT [id], [receivedate], [email]
FROM [mytable]
WHERE [email] IN ( SELECT [email]
FROM [myTable]
GROUP BY [email]
HAVING COUNT([email]) > 1 )
答案 5 :(得分:0)
你想要最后一项的清单吗?如果是这样,你可以使用:
SELECT [info] FROM [table] t WHERE NOT EXISTS (SELECT * FROM [table] tCheck WHERE t.date > tCheck.date)
如果您想要所有重复电子邮件地址的列表,请使用GROUP BY收集类似数据,然后使用HAVING子句确保数量超过1:
SELECT [info] FROM [table] GROUP BY [email] HAVING Count(*) > 1 DESC
如果您想要最后一个重复的电子邮件(单个结果),您只需添加“TOP 1”和“ORDER BY”:
SELECT TOP 1 [info] FROM [table] GROUP BY [email] HAVING Count(*) > 1 ORDER BY Date DESC
答案 6 :(得分:0)
如果您有代理键,则使用SQLMenance帖子中提到的语法相对容易。基本上,由两个或多个行“相同”的所有字段组合。
删除重复记录的伪代码示例。
Create table people (ID(PK), Name, Address, DOB)
Delete from people where id not in (
Select min(ID) from people group by name, address, dob
)