db2删除完全相同的重复数据_DB2数据库重复记录删除sql语句

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值