sql - Comparing consecutive rows using oracle -
i have table looks this:
| id | from_date | to_date | ------------------------------ | 1 | 1/1/2001 | 2/1/2001| | 1 | 2/1/2001 | 3/1/2001| | 1 | 2/1/2001 | 6/1/2001| | 1 | 3/1/2001 | 4/1/2001| | 2 | 1/1/2001 | 2/1/2001| | 2 | 1/1/2001 | 6/1/2001| | 2 | 2/1/2001 | 3/1/2001| | 2 | 3/1/2001 | 4/1/2001|
it sorted id
, from_date
, to_date
.
what want delete rows from_date
earlier to_date
previous line , id equal id previous line. in example, delete 3rd , 6th rows only.
i know need kind of looping structure accomplish this, don't know how since i'm looking @ 2 rows @ time here. how can accomplish within oracle?
edit: using 'lag' function quicker , easier, end deleting 4th , 7th rows - not want do. example, when gets row 4, should compare 'from_date' 'to_date' row 2 (instead of row 3, because row 3 should deleted).
you use lag
window function identify these rows:
delete mytable rowid in (select rowid (select rowid, from_date, lag(to_date) on (partition id order from_date, to_date) lag_to_date my_table) t from_date < lag_to_date)
Comments
Post a Comment