【问题标题】:identify similar rows and delete one with different predicate识别相似的行并删除具有不同谓词的行
【发布时间】:2014-06-27 07:47:00
【问题描述】:

我有一个 Oracle 表,其中包含以下列:

ID (PK) Column_1 Column_2 Column_3 Column_4(仅值为 0 或 1)

我需要通过Column_1、Column_2、Column_3在同一张表中找到匹配的行,并删除Column_4不为0的所有匹配行

我该怎么做?

【问题讨论】:

    标签: sql database oracle


    【解决方案1】:
    delete from table_x
    where exists
      (select * from table_x t2
        where t2.id!=table_x.id
          and t2.column_1=table_x.column_1
          and t2.column_2=table_x.column_2
          and t2.column_3=table_x.column_3)
        and table_x.column_4!=0
    

    t2.id!=table_x.id 是必需的,因此记录与自身不匹配。

    请注意,对于上面的查询,如果您有两行在第 1、2 和 3 列上匹配,并且都有第 4 列!= 0,它将删除两者,因为两者都有匹配的记录,并且和 column_4 !=0。通过对您上面所说的内容的严格字面阅读,我认为这就是您想要的。如果这不是您的意思,您可能需要对查询进行一些更改。

    与任何可能删除大量记录的查询一样,我会制作一个表的副本以进行测试或制定其他备份计划,以防结果不符合您的预期。 :-)

    【讨论】:

      猜你喜欢
      • 2021-02-14
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多