oracle数据库怎么删除重复数据只留一个?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle数据库怎么删除重复数据只留一个?相关的知识,希望对你有一定的参考价值。
表名是 dj_clr 里面有2400条的数据 有主键ID、DJ_MC、DJ_DH、LRRQ(录入日期)这四个字段,表里有很多DJ_MC和DJ_DH相同的数据,但是并不是都是一样的,(比如DJ_MC=1和DJ_DH=2的有三条,DJ_MC=3和DJ_DH=7有两条)这样的,我想把他们去重只留下一条怎么写sql啊,各位大神们求助啊
用Group by 可以将数据进行分组。每组相同数据中只会有一个追问delete from dj_clr where rowid not in (select min(rowid) from dj_clr group by dj_mc,dj_dh); 我这样删可不可以?
参考技术A delete from tablewhere not exists(select 1 from table x where x.col=table.col and x.rowid <> table.rowid group by x.col);
根据rowid去删除吧。 参考技术B 直接执行该sql就可以了:
delete from dj_clr a
where (a.dj_mc,a.dj_dh) in
(select dj_mc,dj_dh
from dj_clr group by dj_mc,dj_dh having count(*) > 1)
and rowid not in
(select min(rowid) from dj_clr group by dj_mc,dj_dh having count(*)>1);本回答被提问者采纳
SQL如何删除2个字段的重复数据,如果重复了就只留一条数据,其他的删除,没有ID字段,在线等....急
只能用一条sql实现
参考技术A 提供一种思路:新建一个表,结构同你要整理的表,
1、把有重复的记录取一条插入新表
2、删除旧表的有重复的数据
3、把新表的数据插入旧表
4、删除新表完工追问
哥哥,这样做,还不如直接把数据复制到临时表
参考技术B 这是我以前写的实际sql,可参考这个思路:DELETE a
-- SELECT *
FROM dm_s_pt a
INNER JOIN
(
SELECT * FROM (
SELECT a.*,
ROW_NUMBER() OVER (PARTITION BY a.Batch,a.Index_code,a.Period,a.Region_code,
a.Operator_code,a.Business,a.Technology,a.Customer_type
order by b.Dp_id ASC) AS Row_Num
FROM dm_s_pt a INNER JOIN dm_s_pt b
ON a.Batch = b.Batch
AND a.Index_code = b.Index_code
AND a.Period=b.Period
AND a.Region_code = b.Region_code
AND ISNULL(a.Operator_code,'') = ISNULL(b.Operator_code,'')
AND ISNULL(a.Business,'') = ISNULL(b.Business,'')
AND ISNULL(a.Technology,'') = ISNULL(b.Technology,'')
AND ISNULL(a.Customer_type,'') = ISNULL(b.Customer_type,'')
AND ISNULL(a.Data_flag,'-1') = ISNULL(b.Data_flag,'-1')
AND a.Dp_id<>b.Dp_id
AND a.Data_flag='SOURCE'
) t WHERE Row_Num>1 /*挑出重复行并编号,然后删掉Row_Num>1的行(只保留编号为1的行)*/
) b
ON a.Batch = b.Batch
AND a.Index_code = b.Index_code
AND a.Period=b.Period
AND a.Region_code = b.Region_code
AND ISNULL(a.Operator_code,'') = ISNULL(b.Operator_code,'')
AND ISNULL(a.Business,'') = ISNULL(b.Business,'')
AND ISNULL(a.Technology,'') = ISNULL(b.Technology,'')
AND ISNULL(a.Customer_type,'') = ISNULL(b.Customer_type,'')
AND ISNULL(a.Data_flag,'-1') = ISNULL(b.Data_flag,'-1')
AND ISNULL(a.Dp_id,'-1') = ISNULL(b.Dp_id,'-1')
AND ISNULL(a.Value,'-999') = ISNULL(b.Value,'-999') 参考技术C select col1,col2,count(*)
from tab
group by col1,col2
having count(*)=1
union all
select col1,col2,count(*)
from tab
group by col1,col2
having count(*)>1追问
哥哥,其他的数据是删除,不是要你查询出只有一条的数据,而且只能用一句SQL语句,谢谢
参考技术D 删除重复数据必须保证每条数据有一个列来区分特殊性,一般都有id字段来区分,DELETE FROM TABLE_NAME
WHERE id 第5个回答 2011-12-31 delete from table_name where (col1,col2) not in (select max(col1),max(col2) from table_name group by col1,col2)追问
delete from tb_Users where (Names,Age) not in (select max(Names),max(Age) from tb_Users group by Names,Age)
为什么这段代码总显示,处 错误?
delete from tb_Users where rowid not in (select max(rowid) from a group by Names,Age)
如果完全没区别 一条语句是做不到的 我这是oracle的写法 rowid这物理地址肯定是唯一的 如果是其他的 目前应该没有一条语句就能解决的办法
rowID不行,没有这个函数,谢谢了
追答rowid不是函数 是oracle给每个行数据的物理地址字段 如果是其他数据库 真没能一个sql就能解决的办法
以上是关于oracle数据库怎么删除重复数据只留一个?的主要内容,如果未能解决你的问题,请参考以下文章