I have a table which have primarily 3 columns I am interested in: old_item_id
, new_item_id
and date_of_change
. I want to traverse the sequence and want to find out latest id
of some item ids. Example data below:
old_item_id new_item_id date_of_change
1 2 2015-01-01
2 5 2015-01-02
5 12 2015-10-01
4 5 2015-01-02
6 7 2015-02-02
So if I want the latest ids of say item 1, 4, 6 and 8; in such case I should get output of:
item_id latest_item_id
1 12
4 12
6 7
8 8
as 1 and 4 could be traced to 12. item id 6 got changed to 7 and item id 8 was never changed.
Currently I am doing this by repeatedly hitting the table in a while loop from another script. However I am looking for query to do it in single database hit.