2014-02-08 173 views
0

我有一个表有两个colums,id和日期。这里是相同的样本数据。删除oracle中的重复记录

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

如何更正?谢谢

+1

Noooooooo!不是另外删除重复的问题!我们如何删除重复的“删除重复”问题? – tbone

回答

1

这适用于sql服务器;

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) 
0

尝试使用子查询exists

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