【问题标题】:remove duplicate records in oracle删除oracle中的重复记录
【发布时间】:2014-02-08 01:55:31
【问题描述】:

我有一个表,它有两个列,id 和 date。这是相同的样本数据。

ID          DATE
1           01-Jan -14 05.42.23.000000000 pm
1           01-Jan -14 05.06.17.000000000 pm
2           01-Jan -14 05.26.16.000000000 pm
2           01-Jan -14 05.41.20.000000000 pm
3           01-Jan -14 05.21.19.000000000 pm
3           01-Jan -14 05.08.18.000000000 pm
4           01-Jan -14 05.14.17.000000000 pm
4           01-Jan -14 05.17.17.000000000 pm

ID 有重复数据需要删除,我想保留列DATE 更大的行。

我写了 SQL 但结果不正确。

delete from newproducts a
 where a.id in
       (select t.id from newproducts t group by t.id having count(*) > 1)
   and a.date not in
       (select max(t.date) from newproducts  t group by t.id having count(*) > 1);

如何纠正?谢谢

【问题讨论】:

  • 不!不是另一个删除重复的问题!我们如何删除重复的“删除重复”问题?

标签: sql oracle


【解决方案1】:

这适用于sql server;

delete a from newproducts as a
 where 
exists(
select * from newproducts b
where a.id = b.id and a.date < b.date)

相同或以下应该适用于 oracle;

delete from newproducts a
 where 
exists(
select * from newproducts b
where a.id = b.id and a.date < b.date)

【讨论】:

    【解决方案2】:

    尝试使用exists 子查询:

    delete from newproducts np
        where not exists (select 1
                          from newproducts np2
                          where np2.id = np.id and np2.date > np.date
                         );
    

    【讨论】:

      猜你喜欢
      • 2019-11-25
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2021-03-17
      • 2021-07-18
      相关资源
      最近更新 更多