How to SQL query through a row hierarchy?

by Holden   Last Updated March 14, 2019 20:26 PM

Suppose I have the following table structure, in an Oracle database, where PARENT references another row in the same table.

  id  | parent
  1   | null
  2   | 1
  3   | 2
  4   | 3
  5   | null
  6   | 5
  7   | 6

How could I query so that each row gets me the end of the sequence. That is I'd like to see this end result:

  id  | end_parent
  1   | 4
  2   | 4
  3   | 4
  4   | 4
  5   | 7
  6   | 7
  7   | 7


Related Questions


Updated July 09, 2017 16:26 PM

Updated February 17, 2018 04:26 AM

Updated March 11, 2019 05:26 AM

Updated March 01, 2017 02:26 AM

Updated January 27, 2019 07:26 AM