一聚教程网:一个值得你收藏的教程网站

热门教程

DB2数据库重复记录删除sql语句

时间:2022-06-29 10:16:37 编辑:袖梨 来源:一聚教程网

--1.查询重复数据

 代码如下 复制代码

select * from tableA where catentry_id in (select distinct catentry_id from tableA group by catentry_id,descitem_id having count(*)>1)@

--2.建立临时表,将所有重复数据存入该表

 代码如下 复制代码

create table detailtemp1 like tableA@

insert into detailtemp1
    select * from tableA
    where catentry_id in (select distinct catentry_id from tableA group by catentry_id,descitem_id having count(*)>1)@
  
select * from detailtemp1@

--3。从原表中删除所有出现重复情况的记录

 代码如下 复制代码

delete from tableA where catentry_id in (select distinct catentry_id from tableA group by catentry_id,descitem_id having count(*)>1)@

--4。用group by 分组,将临时表中的不重复记录插入原表中

 代码如下 复制代码

insert into tableA
select catentry_id,descitem_id,max(content)
from detailtemp1
group by catentry_id,descitem_id@


select * from tableA where catentry_id in (select distinct catentry_id from tableA group by catentry_id,descitem_id having count(*)>1)@

 
--5。删除临时表

 代码如下 复制代码

drop table detailtemp1@

--end

另外再分享几种办法

 2、DB2删除重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

 代码如下 复制代码
delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

  3、查找表中多余的重复记录(多个字段)

 代码如下 复制代码

  select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

  4、DB2删除重复记录(多个字段),只留有rowid最小的记录

 代码如下 复制代码

  delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

  5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

 代码如下 复制代码

  select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

  6. 处理bas_information中去重,根据

 代码如下 复制代码
t.PISField001,t.PISField011,t.areaCode
export to e:/bas_information.txt of del select s.HOUSEHOLDS, s.PISFIELD000, s.PISFIELD001, s.PISFIELD002,
s.PISFIELD003, s.PISFIELD004, s.PISFIELD005, s.PISFIELD006, s.PISFIELD011,
s.PISFIELD012, s.PISFIELD013, s.PISFIELD014, s.PISFIELD015, s.PISFIELD016,
s.DEADDATE, s.QIANRUDATE, s.ZHIYE, s.JIATING, s.AREACODE, s.REG_DATE,
s.ISLOGOUT from (select t.*,rownumber() over(partition by t.PISField001,t.PISField011,t.areaCode) as rn from bas_information t ) s where s.rn = 1;

热门栏目