程序员人生 网站导航

SQL删除重复记录(保留一条)的问题

栏目:Sqlserver时间:2013-10-11 08:20:19

今天,有个网友来请教小编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
)
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐