Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hierarchy: Query Challenge.

572471May 8 2007 — edited May 10 2007

Hello, everyone.

I have a data like this:

with t as (select 1 id, null parent_id, 2 num from dual union all
           select 2, 1, 2 from dual union all
           select 3, 2, 3 from dual union all
           select 4, 1, 1 from dual union all
           select 3, 4, 2 from dual union all
           select 6, 3, 2 from dual union all
           select 7, 3, 1 from dual)

as you can see the node with id = 3 is met twice.
So the tree looks like (with "num" value in square brackets)

        1[2]
        /  \
      2[2]  4[1]
        \  /
         3[3;2]
        /   \
      6[2]  7[1]

Well, the idea is to find a query that will return the product
of "num" values of all the ancestors, including "num" value of the current node.
E.g. on the provided data the result would be:

        ID  PARENT_ID        NUM    PRODUCT
---------- ---------- ---------- ----------
         1                     2          2
         2          1          2          4
         3          2          3         12
         3          4          2          4
         4          1          1          2
         6          3          2         24
         6          3          2          8
         7          3          1         12
         7          3          1          4

Mention that nodes with id in (3,6,7) are returned twice because they have two different pathes to the root level.

Thanks beforehand.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 7 2007
Added on May 8 2007
18 comments
1,577 views