Find latest replacement_id in a table

See this answer: Building Org Structure - Parent_Child Structure - #8 by dgudkov