Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: August 06, 2009 - 8:14 pm UTC

Last updated: August 07, 2009 - 1:55 pm UTC

Version: 9.0.2 and 10g

Viewed 1000+ times

You Asked

I have the following table:
create table geometry_sample(geo_id number, geo_parent_id number, geo_path varchar2(512), geo_value varchar2(512));


with the following values:

insert into geometry_sample values (101, null, '/geometries', 'geometries');
insert into geometry_sample values (102, 101, '/geometries/shapes', 'shapes');
insert into geometry_sample values (103, 102, '/geometries/shapes/boxes', 'boxes');
insert into geometry_sample values (104, 102, '/geometries/shapes/curves', null);
insert into geometry_sample values (105, 103, '/geometries/shapes/boxes/rectangles', null);
insert into geometry_sample values (106, 104, '/geometries/shapes/curves/arc', null);
commit;



How do I get the values to be 'inherited' by the children if they don't have a value, so that the result looks like the following (the values marked in bold are inherited from their parents - if the immediate parent doesn't have a value, it should 'climb' up the ladder) - it is not known how many levels the hierarchy will have - but we can safely assume that it will never go beyond 50, if that is useful information. However if the solution doesn't care about the number of levels, that's the most ideal.

101 /geometries geometries
102 101 /geometries/shapes shapes
103 102 /geometries/shapes/boxes boxes
104 102 /geometries/shapes/curves shapes
105 103 /geometries/shapes/boxes/rectangles boxes
106 104 /geometries/shapes/curves/arc shapes


Thank you!


and Tom said...

ops$tkyte%ORA10GR2> select g.*,
  2         nvl( geo_value,
  3             (select geo_value
  4                 from geometry_sample g2
  5                where geo_value is not null
  6                start with g2.geo_id = g.geo_parent_id
  7              connect by prior g2.geo_parent_id = g2.geo_id
  8               and prior geo_value IS NULL) ) geo_value2
  9    from geometry_sample g
 10   order by geo_id
 11  /

    GEO_ID GEO_PARENT_ID GEO_PATH                                 GEO_VALUE       GEO_VALUE2
---------- ------------- ---------------------------------------- --------------- ---------------
       102           101 /geometries/shapes                       shapes          shapes
       103           102 /geometries/shapes/boxes                 boxes           boxes
       104           102 /geometries/shapes/curves                                shapes
       105           103 /geometries/shapes/boxes/rectangles                      boxes
       106           104 /geometries/shapes/curves/arc                            shapes


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

terrific

A reader, August 10, 2009 - 2:42 pm UTC

Tom,
Thank you very much for a terrific solution - it looks so neat and simple! I had been trying to fashion a solution using sys_connect_by_path, substrings, levels - I just couldn't make the leap.
Allow me to also take this opportunity to express my sincere appreciation for the numerous other tricks/solutions/concepts I have freely adopted from this site - my apologies for never even dropping a note of thanks before!