2014-02-08 237 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 
        );