我有一张表有两个列,id和日期。这是相同的样本数据。
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);
如何纠正?感谢
答案 0 :(得分: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)
答案 1 :(得分:0)
尝试使用exists
子查询:
delete from newproducts np
where not exists (select 1
from newproducts np2
where np2.id = np.id and np2.date > np.date
);