首页 > 数据库 >SQLServer去重复数据

SQLServer去重复数据

时间:2022-12-09 19:01:01浏览次数:66  
标签:seasonId List dbo 重复 Season SQLServer LZ 数据 HD

删除表中多余的重复记录,重复记录是根据单个字段(seasonId )来判断,只留有rowid最小的记录

分析: 

select seasonId ,count(1) FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId  having count(1)>1 order by seasonId

 select min(id)  FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId  having count(1)>1  order by seasonId

执行SQL

 delete from [HD_GameData].[dbo].[LZ_Season_List] where seasonId

 in ( select seasonId  FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId  having count(1)>1 )

 and

 id not in (  select min(id)  FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId  having count(1)>1)


查找表中多余的重复记录(多个字段,seasonId ,sportId )

select seasonId ,sportId FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId,sportId   having count(1)>1 order by seasonId

 select min(id)  FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId, sportId having count(1)>1  order by seasonId

执行SQL:

 delete from [HD_GameData].[dbo].[LZ_Season_List] where (seasonId,sportId)

 in ( select seasonId ,sportId FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId,sportId   having count(1)>1 )

 and

 id not in (  select min(id)  FROM [HD_GameData].[dbo].[LZ_Season_List] group by seasonId,sportId     having count(1)>1)

标签:seasonId,List,dbo,重复,Season,SQLServer,LZ,数据,HD
From: https://blog.51cto.com/jition/5926389

相关文章