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 8821477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:51:54+00:00 2026-06-14T05:51:54+00:00

I need to get the children of a given node, along with a field

  • 0

I need to get the children of a given node, along with a field that belongs to their highest ancestor. I know how to get both, individually, and I could probably figure out how to programatically combine the two later; but I’d prefer to do it all in one query.

Assuming my table includes the fields id, item_id, code, and name, and that an item_id of 0 is a root node, and that my tree looks like:

id   item_id  code      name    level
------------------------------------
1    0                  Root    1
2    1        my_value  Child1  2
3    2                  Child2  3

I can get the nodes I need using:

select id, item_id, code, name, level from my_table
start with code like '%my_value%'
connect by prior id = item_id

which returns:

id   item_id  code      name    level
-------------------------------------
2    1        my_value  Child1  2
3    2                  Child2  3

I’d like to also get the name field from the highest ancestor(s) of these nodes. I can get those nodes using:

select id, item_id, code, name, level from my_table
where level = (
    select max(level) as max_level from my_table
    start with code like '%my_value%'
    connect by prior item_id = id
)
start with code like '%my_value%'
connect by prior item_id = id

which returns:

id   item_id  code      name    level
-------------------------------------
1    0                  Root    1

I’d like to add the name field returned from the above query as an additional field added to the two nodes returned from the first query, i.e.

id   item_id  code      name    root_name   level
------------------------------------------------
2    1        my_value  Child1  Root        2
3    2                  Child2  Root        3

I hope that’s clear. Any suggestions?

  • 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-14T05:51:55+00:00Added an answer on June 14, 2026 at 5:51 am

    perhaps start from the root, and then filter afterwards to limit to starting with codes like my_value?

    SQL> select id, item_id, code, name, lvl, root_name
      2    from (select id, item_id, code, name, level lvl, sys_connect_by_path(code, '|') c,
      3                connect_by_root name as root_name
      4             from my_table
      5            start with item_id = 0
      6           connect by prior id = item_id)
      7   where c like '%my_value%';
    
            ID    ITEM_ID CODE                 NAME              LVL ROOT_NAME
    ---------- ---------- -------------------- ---------- ---------- ----------
             2          1 my_value             Child1              2 Root
             3          2                      Child2              3 Root
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to get an div with overflow:hidden; not to wrap its children. I need
Need to get the 10 word before and 10 words after for the given
Given a valid, arbitrary xmlNodePtr, I would like the string representation of that node,
I need a tree structure that supports and and oring. For example, given a
I have this function that returns me all children of a tree node: CREATE
I need get the id of an images which is place inside a datatemplate..
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I need get all items these have no categories int? categoryId = null; var
I have linq request. I need get item.Title in select. how do this? var
I have two tables with a weak relation. I need get a text value

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.