步步为营-89-SQL语句(删除重复数据)

1:删除重复数据

--第一步:先找到重复数据
select ProcInstID from record_errorlog
group by ProcInstID
having count(ProcInstID) > 1
--查看一下
select * from record_errorlog
where ProcInstID in (select ProcInstID from record_errorlog
group by ProcInstID
having count(ProcInstID) > 1);
--第二部 保留ErrorLogID最大的(也就是最新的数据)
delete  from record_errorlog
where ProcInstID in (select ProcInstID from record_errorlog group by ProcInstID having count(ProcInstID) > 1)
and ErrorLogID not in (select max(ErrorLogID) from  record_errorlog group by ProcInstID having count(ProcInstID )>1);

时间: 2024-10-09 00:51:54

步步为营-89-SQL语句(删除重复数据)的相关文章

SQL语句删除重复数据

1.如表中没有主键,先添加自动增长主键 alter table 表名 add 列名 int identity (1,1) primary key 2.删除重复数据 delete from 表名 where id not in (select min(id) from 表名 group by id)

sql查询删除重复数据

数据库UserInfo 删除重复数据 即删除重复的用户名手机号 同一个用户名手机号只保留一个用户 01.根据多个字段查询重复数据 with data1 as( select MobilePhone,Name from UserInfogroup by MobilePhone,Namehaving count(*)>1 ), 02.对重复数据分配编号 data2 as ( select u.*,row_number() over(partition by u.MobilePhone,u.Name

Sql语句——删除表数据drop、truncate和delete的用法

一.SQL中的语法 1.drop table 表名称                         eg: drop table  dbo.Sys_Test   2.truncate table 表名称                     eg: truncate  table dbo.Sys_Test                     3.delete from 表名称 where 列名称 = 值      eg: delete from dbo.Sys_Test where te

mysql 删除重复数据的sql语句

CREATE TABLE tmp AS SELECT id FROM get_review_url WHERE (no,title,name,content) IN (SELECT no,title,name,content FROM get_review_url GROUP BY no,title,name,content HAVING COUNT(*) > 1) AND id NOT IN (SELECT MIN(id) FROM get_review_url GROUP BY no,tit

sql语句查询重复的数据

查找所有重复标题的记录:SELECT *FROM t_info aWHERE ((SELECT COUNT(*)FROM t_infoWHERE Title = a.Title) > 1)ORDER BY Title DESC一.查找重复记录1.查找全部重复记录Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)2.过滤重复记录(只显示一条)Select * From HZT Whe

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

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)来判断,只留有

用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

sql删除重复数据

--删除重复数据delete from PMS_Contract_LeasedRoom  where LeasedRoomId not in(select max(LeasedRoomId) from PMS_Contract_LeasedRoom group by ContractCode,RoomCode) --having count(*)>1)