当前位置: 代码迷 >> SQL >> sql 剔除表中多余的重复记录(多个字段),只保留一条记录
  详细解决方案

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

热度:90   发布时间:2016-05-05 12:30:38.0
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 )
)