首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2013-01-26 
sql 删除表中多余的重复记录(多个字段),只保留一条记录SELECT * into PT_PROGRAM_TAG0FROM [IAR_DB].[dbo]

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 )
)

热点排行