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

来源:互联网 发布:c语言中求根号 编辑:程序博客网 时间:2024/04/30 22:40
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 )
)