Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:11:22+00:00 2026-06-12T08:11:22+00:00

I am trying to query a (large) Oracle 9 table that represents some hierarchical

  • 0

I am trying to query a (large) Oracle 9 table that represents some hierarchical data. The Parent items have their own Id as Parent ID. As an example;

ID    PARENTID               
----- --------  
1     1
2     1
3     2
4     2
5     3
6     6
7     6
8     6
9     4
10    10

I want to have a query that returns each ID, along with the ultimate Parent for that ID, so continuing my example

ID    UlitimateParent
----  ----
1     1
2     1
3     1
4     1
5     1
6     6
7     6
8     6
9     1
10    10

I’ve seen a few examples using Connect By but can’t seem to get this to work. Any ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:11:23+00:00Added an answer on June 12, 2026 at 8:11 am

    In 10g+, you would use the CONNECT_BY_ROOT function:

    SQL> with data as (
      2     SELECT 1 id, 1 parent_id FROM DUAL
      3     UNION ALL SELECT 2 , 1  FROM DUAL
      4     UNION ALL SELECT 3 , 2  FROM DUAL
      5     UNION ALL SELECT 4 , 2  FROM DUAL
      6     UNION ALL SELECT 5 , 3  FROM DUAL
      7     UNION ALL SELECT 6 , 6  FROM DUAL
      8     UNION ALL SELECT 7 , 6  FROM DUAL
      9     UNION ALL SELECT 8 , 6  FROM DUAL
     10     UNION ALL SELECT 9 , 4  FROM DUAL
     11     UNION ALL SELECT 10, 10 FROM DUAL
     12  )
     13  SELECT id, connect_by_root(id) ultimate_parent_id
     14    FROM data
     15  START WITH id = parent_id
     16  CONNECT BY parent_id = PRIOR id AND id != PRIOR id  ;
    
    ID  ULTIMATE_PARENT_ID
    --- ------------------
      1                  1
      2                  1
      3                  1
      5                  1
      4                  1
      9                  1
      6                  6
      7                  6
      8                  6
     10                 10
    

    In 9i, you could use SYS_CONNECT_BY_PATH (with appropriate substring):

    SQL> with data as (
      2     SELECT 1 id, 1 parent_id FROM DUAL
      3     UNION ALL SELECT 2 , 1  FROM DUAL
      4     UNION ALL SELECT 3 , 2  FROM DUAL
      5     UNION ALL SELECT 4 , 2  FROM DUAL
      6     UNION ALL SELECT 5 , 3  FROM DUAL
      7     UNION ALL SELECT 6 , 6  FROM DUAL
      8     UNION ALL SELECT 7 , 6  FROM DUAL
      9     UNION ALL SELECT 8 , 6  FROM DUAL
     10     UNION ALL SELECT 9 , 4  FROM DUAL
     11     UNION ALL SELECT 10, 10 FROM DUAL
     12  )
     13  SELECT id, sys_connect_by_path(id, '->') path
     14    FROM data
     15  START WITH id = parent_id
     16  CONNECT BY parent_id = PRIOR id
     17         AND id != PRIOR id;
    
            ID PATH
    ---------- --------------------
             1 ->1
             2 ->1->2
             3 ->1->2->3
             5 ->1->2->3->5
             4 ->1->2->4
             9 ->1->2->4->9
             6 ->6
             7 ->6->7
             8 ->6->8
            10 ->10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large table that I'm trying to break up. For example I
I have a Django query and some Python code that I'm trying to optimize
I'm trying to create a faster query, right now i have large databases. My
I'm trying to generate a query that finds all large, red things with a
I have a large query that joins around 20 tables (mostly outer joins). It
We have some large tables and are trying to optimize against. (Note: in this
I'm trying to accomplish the following: Have a thread that reads data from a
I have a large mysql table and I just noticed an issue when trying
I am using linq to nhibernate to query data from large table. I am
I am trying to run a large script that creates a table, and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.