定义层次结构查询中子项的排序顺序

时间:2012-11-11 21:34:24

标签: sql oracle sql-order-by hierarchy

我想知道通过sequence_within_parent列在层次结构查询中对子项进行排序的Oracle SQL查询。

示例数据集和查询是:

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
/

此查询返回以下内容:

   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

首选输出位于儿童处于正确顺序的位置:

   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

2 个答案:

答案 0 :(得分:3)

要添加到查询中的子句是“按顺序浏览SEQUENCE_WITHIN_PARENT”。

在层次结构中,所有子节点或子节点都称为兄弟节点。

示例数据集的完整查询是:

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
/

答案 1 :(得分:2)

SQL Server有一个hierarchyID类型,可以很好地处理这个问题。对于所有其他RDBMS,我通常使用字符串concat仿真,如下所示。

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;