sql - 甲骨文 : How to delete duplicates rows when no distinct value exists?

标签 sql oracle sql-delete

我们有一个表,其中有许多行重复(2 到 4 次)(每行每列都有相同的值)。 该表有 4 列,但没有主键。

那么如何删除重复的行呢?

Instance  status  updatetime              name
-----------------------------------------------
gdt546    4       2016/02/13 10:13:18     basic
gdt546    4       2016/02/13 10:13:18     basic
ort87a    1       2016/02/16 19:09:43     High
ort87a    1       2016/02/16 19:09:43     High
ort87a    1       2016/02/16 19:09:43     High

已编辑:

输出应该是:

 Instance  status  updatetime              name
-----------------------------------------------
gdt546    4       2016/02/13 10:13:18     basic
ort87a    1       2016/02/16 19:09:43     High

最佳答案

假设表的名称为 TABLE_NAME 并且目的是删除重复行,使其仅保留其中之一,请查找以下查询:

DELETE FROM table_name A 
WHERE  A.ROWID > ANY (SELECT B.ROWID 
                      FROM   table_name B 
                      WHERE  A.INSTANCE = B.INSTANCE 
                             AND A.status = B.status 
                             AND A.updatetime = B.updatetime 
                             AND A.name = B.name); 

关于sql - 甲骨文 : How to delete duplicates rows when no distinct value exists?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35521260/

相关文章:

sql - 是否可以在一列上连接两个表 = 串联 2 列?

sql - 如何使用 group by 语句 + max 函数加速 SQL 查询?

MySQL:截断表与从表中删除

mysql - 删除记录

mysql - 在 MySQL 中将字符串转换为 int

MySQL,错误 126 : Incorrect key file for table

mysql - 根据订单列交换 Oracle 中的两条记录

mysql - 使用SQL执行唯一删除

SQL 类似于正则表达式,相当于开始 ^ 和结束 $ 行

java - 从 PL/SQL 调用 Java 加密体系结构 (JCA)