SQL删除重复记录(保留一条)的问题
来源:程序员人生 发布时间:2013-10-11 08:20:19 阅读次数:3109次
今天,有个网友来请教小编SQL删除重复记录的问题,他的数据库中有大量的重复记录,他只想保留一条,这个问题可把小编难住了,因为小编对SQL SERVER不太熟悉,在网上搜了一下,代码如下:
table sturcture
{taskRecordId,taskDate,userId,teamId,taskId,unit,nots}
delete duplicate taskDate,UserId,teamId and taskId.
the sql is
delete from TaskRecord
select * from TaskRecord
where taskRecordId in
(
select MIN(taskRecordId) from TaskRecord t join
(select COUNT(*) d,taskDate,userId,teamId from TaskRecord
where taskId=6
group by taskDate,userId,teamId having (COUNT(*)>1)) x
on t.taskDate=x.taskDate and t.userId=x.userId and t.teamId
=x.teamId where t.taskId=6 group by t.taskDate,t.userId,t.teamId
)