SqlServer_删除重复行只保留一条记录

前提:相同的数据重复往数据库写入,导致存在仅主键Id不同的重复数据,现在需要去除重复数据,仅保留重复数据中Id最大的一条

思路:

1、找出存在重复数据的记录,并取重复数据中最大的Id值

2、删除记录中不包含最大Id值的记录

实现:

DELETE FROM [Log_IIS].[dbo].[IISLog_table]

WHERE id not in

(select MAX(Id) from [IISLog_table] group by [RequestTime]

,[LogFilename]

,[LogRow]

,[date]

,[time]

,[cIp]

,[csUsername]

,[sSitename]

,[sComputername]

,[sIp]

,[sPort]

,[csMethod]

,[csUriStem]

,[csUriQuery]

,[scStatus]

,[scSubstatus]

,[scWin32Status]

,[scBytes]

,[csBytes]

,[timeTaken]

,[csVersion]

,[csHost]

,[csUserAgent]

,[csCookie]

,[csReferer]

,[sEvent]

,[sProcessType]

,[sUserTime]

,[sKernelTime]

,[sPageFaults]

,[sTotalProcs]

,[sActiveProcs]

,[sStoppedProcs] having count(*) > 1)   

来自为知笔记(Wiz)

时间: 2024-12-09 00:36:33

SqlServer_删除重复行只保留一条记录的相关文章

SQL删除重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有r

MySQL中删除重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT * FROM people WHERE peopleId IN ( SELECT peopleId FROM people GROUP BY peopleId HAVINGcount(peopleId) > 1 ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3

Oralce中SQL删除重复数据只保留一条(转)

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有

MySQL删除重复数据只保留一条

面试碰到一个MySQl的有趣的题目,如何从student表中删除重复名字的行,并保留最小id的记录? 很遗憾当时没有做出来,回家搜索了一番,发现利用子查询的可以很快解决. 1.删除表中多余的重复记录,重复记录是username判断,只留有id最小的记录 delete from studentwhere username in ( select username from studentgroup by username having count(username)>1) and id not i

MSSQL 删除重复并只保留一条

重复值查找 select field1,field2 from table group by field1,field2 having COUNT(*)>1 删除重复值,只保留ID最小的那条 delete table where field1 in (select field1 from VersionProductRelation a group by field1,field2 having COUNT(*)>1) and field2 in (select field2 from Ver

SQL删除重复数据只保留一条数据

1.表结构与数据: CREATE TABLE tablezzl( id int, name VARCHAR(255) ); 2.查询出重复的数据: SELECT name FROM tablezzl GROUP BY name HAVING COUNT(name)>1 3.查询出要保留的重复数据: SELECT MIN(id) ids FROM tablezzl GROUP BY NAME HAVING COUNT(name)>1 4.最终的SQL: DELETE FROM tablezzl

oracle 删除掉重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有

Access数据库删除重复记录,只保留一条记录的做法

Access数据库删除重复记录,只保留一条记录的做法: 只保留id最小的记录方法: delete from [表名] where id not in (select min(id) from [表名] group by [带重复记录的字段名称]) 只保留id最大的记录方法: delete from [表名] where id not in (select max(id) from [表名] group by [带重复记录的字段名称])

用SQL语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 delete fr