I have a table with the following format:
pk fk parent
===== ====== ========
001 23 000
002 null 001
003 46 001
004 12 000
005 null 004
006 null 005
=====================
pk
is the primary key for each row, fk
is a foreign key to another table I'll need to join later, and parent
is the record's hierarchical parent. I can create a hierarchical query on this data with
select
lpad(' ',2*level)||pk "primary_key",
fk "foreign_key",
sys_connect_by_path(pk,'/') "path"
from example_table
connect by prior pk = parent
;
My question is how do I overwrite a row's null
foreign key with that of the lowest ancestor without a null? My intended output in this scenario is
pk fk parent
========= ==== ======
001 23 000
002 23 001
003 46 001
004 12 000
005 12 004
006 12 005
====================
(primary keys padded to show hierarchical structure.)
You could express this with a standard recursive common table expression:
Demo on DB Fiddlde: