sql删除表中多余的重复记录(多个字段),只保留一条记录

2014-11-24 17:01:14 · 作者: · 浏览: 1

sql删除表中多余的重复记录(多个字段),只保留一条记录
SELECT * into PT_PROGRAM_TAG0
FROM [IAR_DB].[dbo].[PT_PROGRAM_TAG]
with aa
as(
select count(*) as c from PT_PROGRAM_TAG0 group by PROGRAMID,TagID,TypeID having count(*) > 1
)
select SUM(c) from aa
with b as(select PROGRAMID,TagID,TypeID,MIN(id) as minid from PT_PROGRAM_TAG group by PROGRAMID,TagID,TypeID having count(*) > 1)
delete [PT_PROGRAM_TAG] where ID in(
select a.id from PT_PROGRAM_TAG a
where exists(select PROGRAMID,TagID,TypeID from b where PROGRAMID=a.PROGRAMID and TagID=a.TagID and TypeID=a.TypeID and a.id<>b.minid )
)