问题
we have a scenario here where we need to delete all the duplicate rows from a table based on timestamp. The table structure looks like this:
Item Ref1 Ref2 Timestamp 1 A test1 2/3/2012 10:00:00 1 A test2 2/3/2012 11:00:00 1 A test1 2/3/2012 12:00:00 2 A prod1 2/3/2012 10:00:00 2 B prod2 2/3/2012 11:00:00 2 A prod2 2/3/2012 12:00:00
So we need to delete the duplicate rows from this table based on item and ref1. like here we should have only 1 row for item 1 and ref1 A with the latest timestamp. Same for item 2 we should have only 1 row for ref1 A with latest timestamp.
Any pointers will be great
回答1:
Assuming that your desired end result is a table with these 3 rows
Item Ref1 Ref2 Timestamp
1 A test1 2/3/2012 12:00:00
2 B prod2 2/3/2012 11:00:00
2 A prod2 2/3/2012 12:00:00
Something like
DELETE FROM table_name a
WHERE EXISTS( SELECT 1
FROM table_name b
WHERE a.item = b.item
AND a.ref1 = b.ref1
AND a.timestamp < b.timestamp );
should work assuming that there are no two rows with the same Item and Ref1 that both have the same Timestamp. If there can be multiple rows with the same Item and Ref1 that both have the latest Timestamp and assuming that you don't care which one you keep
DELETE FROM table_name a
WHERE EXISTS( SELECT 1
FROM table_name b
WHERE a.item = b.item
AND a.ref1 = b.ref1
AND a.timestamp <= b.timestamp
AND a.rowid < b.rowid);
回答2:
You can query your records grouping by Item and Ref1 an then delete where Item and Ref are equals and Timestamp < max.
select Item
, Ref1
, max(Timestamp) tm
from table
group by Item, Ref1
With the results...
delete from table where Item = ? and Ref1 = ? and Timestamp < ?
回答3:
I don't have an Oracle 9 install at hand, so I cannot test this, but I believe that this may work:
Create a view which lists adds "indices" to your records:
SELECT ROW_NUMBER( ) OVER (PARTITION BY Item, Ref1 ORDER BY Timestamp DESC) ix, * FROM tableDelete the records from the view where
ixis higher than 1
来源:https://stackoverflow.com/questions/9133277/pl-sql-oracle-9i-deleting-duplicate-rows-using-sql