8

I would like to know the Oracle SQL query that orders the children in a hierarchy query by the sequence_within_parent column.

An example data set and query are:

create table tasks (task_id                 number
                    ,parent_id               number
                    ,sequence_within_parent number
                    ,task                    varchar2(30)
                    );
insert into tasks values ( 1, NULL, 0, 'Task 1');
insert into tasks values ( 2,    1, 1, 'Task 1.1');
insert into tasks values ( 3,    1, 2, 'Task 1.2');
insert into tasks values ( 4,    2, 2, 'Task 1.1.2');
insert into tasks values ( 5,    3, 1, 'Task 1.2.1');
insert into tasks values ( 6,    2, 1, 'Task 1.1.1');
insert into tasks values ( 7,    3, 4, 'Task 1.2.4');
insert into tasks values ( 8,    3, 2, 'Task 1.2.2');
insert into tasks values ( 9,    3, 3, 'Task 1.2.3');
insert into tasks values (10 ,   2, 3, 'Task 1.1.3');

column task format a30

select task_id
      ,sequence_within_parent
      ,lpad(' ', 2 * (level - 1), ' ') || task task
from   tasks
connect by parent_id = prior task_id
start with task_id = 1
/

This query returns the following:

   TASK_ID SEQUENCE_WITHIN_PARENT TASK
---------- ---------------------- ---------------
         1                      0 Task 1
         2                      1   Task 1.1
         4                      2     Task 1.1.2
         6                      1     Task 1.1.1
        10                      3     Task 1.1.3
         3                      2   Task 1.2
         5                      1     Task 1.2.1
         7                      4     Task 1.2.4
         8                      2     Task 1.2.2
         9                      3     Task 1.2.3

The preferred output is below where the children are in the correct order:

   TASK_ID SEQUENCE_WITHIN_PARENT TASK
---------- ---------------------- ---------------
         1                      0 Task 1
         2                      1   Task 1.1
         6                      1     Task 1.1.1
         4                      2     Task 1.1.2
        10                      3     Task 1.1.3
         3                      2   Task 1.2
         5                      1     Task 1.2.1
         8                      2     Task 1.2.2
         9                      3     Task 1.2.3
         7                      4     Task 1.2.4
0

2 Answers 2

3

The clause to be added to the query is "ORDER SIBLINGS BY SEQUENCE_WITHIN_PARENT".

In the hierarchy all child nodes, or children, are referred to as siblings.

The full query for the example dataset is:

select rownum
      ,task_id
      ,sequence_within_parent
      ,lpad(' ', 2 * (level - 1), ' ') || task  task
from   tasks
connect by parent_id = prior task_id
start with task_id = 1
order siblings by sequence_within_parent
/
2
  • On very simple queries, ORDER SIBLINGS BY would probably work. asktom.oracle.com/pls/apex/… has some queries (double connect by prior) that won't. Nor does it give you much flexibility, which using the zero-padded strings would. Won't matter if you use Oracle >= 9i, but connect siblings by didn't exist then Nov 11, 2012 at 22:06
  • you can accept your own answer after x? hours and gain reputation points. Also fair to upvote other answers that give your a new prespective. Good luck to all.
    – shellter
    Nov 11, 2012 at 22:19
2

SQL Server has a hierarchyID type that handles this very well. For all other RDBMS, I normally use a string concat emulation as below.

select task_id
      ,sequence_within_parent
      ,lpad(' ', 2 * (level - 1), ' ') || task task
      ,SYS_CONNECT_BY_PATH(
        to_char(parent_id, 'FM000000000')
        ||
        to_char(sequence_within_parent, 'FM000000000')
        ,'/') hier
from   tasks
connect by parent_id = prior task_id
start with task_id = 1
order by hier;
2
  • Thank you for an excellent variation on the theme. To complete the initial request I would add. "column hier noprint".
    – Tai Paul
    Nov 11, 2012 at 22:25
  • Note from Oracle documentation: "In a hierarchical query, do not specify either ORDER BY or GROUP BY, as they will destroy the hierarchical order of the CONNECT BY results. If you want to order rows of siblings of the same parent, then use the ORDER SIBLINGS BY clause." see halfway this page docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm
    – Joram
    Oct 13, 2016 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.