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

  • Home
  • SEARCH
  • 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 220241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:55:13+00:00 2026-05-11T18:55:13+00:00

I have a hierarchical query to track a reporting structure. This almost works, except

  • 0

I have a hierarchical query to track a reporting structure. This almost works, except that it’s not reporting the very top level node, probably because the top-level people “report” to themselves.

The query is:

select
  level,
  empid, 
  parentid
from usertable
connect by nocycle prior parentid= empid
start with empid = 50

This produces:

LEVEL  EMPID PARENTID               
------ ----- --------  
1      50    258            
2      258   9555
3      9555  17839

I’m not getting a level 4, since it would look like:

4      17839 17839

Without changing data, is there a way to modify my query so that all 4 levels are returned? The goal is to get the empids, so I can do a check for

id in (hierarchical subquery)

BTW, if I remove the nocycle from the query I get an error.

  • 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-05-11T18:55:13+00:00Added an answer on May 11, 2026 at 6:55 pm

    Chris,

    You only get 3 rows because your top level row is not set the way it should to handle hierarchical queries. Typically the top level row, or president KING in Oracle’s well known EMP table, has no manager. In your case you should not set the parentid of 17389 to 17389 itself, but to NULL. Either update the table accordingly, or use a view to accomodate for this situation.

    An example:

    SQL> select empno
      2       , mgr
      3    from emp
      4   where empno in (7876,7788,7566,7839)
      5  /
    
         EMPNO        MGR
    ---------- ----------
          7566       7839
          7788       7566
          7839       7839
          7876       7788
    
    4 rijen zijn geselecteerd.
    

    This part of the EMP table has four levels with its top level row (7839) set to itself. The same as your empid 17839. And this leads to only three rows using your query:

    SQL>  select level
      2        , empno
      3        , mgr
      4     from emp
      5  connect by nocycle prior mgr = empno
      6    start with empno = 7876
      7  /
    
         LEVEL      EMPNO        MGR
    ---------- ---------- ----------
             1       7876       7788
             2       7788       7566
             3       7566       7839
    
    3 rijen zijn geselecteerd.
    

    Either use a (inline) view to set the mgr/parentid column to null for the top level:

    SQL>  select level
      2        , empno
      3        , mgr
      4     from ( select empno
      5                 , nullif(mgr,empno) mgr
      6              from emp
      7          )
      8  connect by nocycle prior mgr = empno
      9    start with empno = 7876
     10  /
    
         LEVEL      EMPNO        MGR
    ---------- ---------- ----------
             1       7876       7788
             2       7788       7566
             3       7566       7839
             4       7839
    
    4 rijen zijn geselecteerd.
    

    Or fix your data with an UPDATE statement:

    SQL> update emp
      2     set mgr = null
      3   where empno = 7839
      4  /
    
    1 rij is bijgewerkt.
    
    SQL>  select level
      2        , empno
      3        , mgr
      4     from emp
      5  connect by nocycle prior mgr = empno
      6    start with empno = 7876
      7  /
    
         LEVEL      EMPNO        MGR
    ---------- ---------- ----------
             1       7876       7788
             2       7788       7566
             3       7566       7839
             4       7839
    
    4 rijen zijn geselecteerd.
    

    And you can leave out the NOCYCLE keyword as well, after you are done fixing.

    Regards,
    Rob.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchical query in Oracle 10 SQL that used to work. However,
I have a class that contains hierarchical data. I want to present this data
I have a typical SQL Server hierarchical query: WITH bhp AS ( SELECT name,
I have an issue with jquery where elements are not found when the query
I have some hierarchical data - each entry has an id and a (nullable)
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have hierarchical data stored in an XML file. There are multiple companies, each
I have hierarchical data in a nested set model (table:projects): My table (projects): id,
I have an issue. I have hierarchical XML data such as: <Tree> <Node Text=Stuff
In SQL server 2008 I have a hierarchical field (hierarchyid). How do I change

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.