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

Ask A Question

Stats

  • Questions 192k
  • Answers 192k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Easy. Don't add up the number of candies. Instead, keep… May 12, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer Have the dividers background image in the content are and… May 12, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer RewriteRule [a-zA-Z_-]+/([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L] RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L] May 12, 2026 at 6:21 pm

Related Questions

Given SQL as an input, I have to query a PostgreSQL database and return
I have a view that will look like this: I'm trying to figure out
I have a SQL table like so: Update: I'm changing the example table as
I have 5 tables in a L2S Classes dbml : Global >> Categories >>

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.