为什么 oracle SYS_CONNECT_BY_PATH return 为空值?
Why does oracle SYS_CONNECT_BY_PATH return null values?
为什么我的 PATH 列充满了空值?分层数据中的循环是否会导致 SYS_CONNECT_BY_PATH 函数出现问题?
Oracle 11g R2 架构设置:
CREATE TABLE MODULES
(
MOD_ID INTEGER NOT NULL,
NAME VARCHAR2(20) NOT NULL,
PRNT_MOD_ID INTEGER
);
INSERT INTO MODULES VALUES(1, 'BASE MODULE', 1);
INSERT INTO MODULES VALUES(2, 'SUB MODULE 1-2', 1);
INSERT INTO MODULES VALUES(3, 'SUB MODULE 1-3', 1);
INSERT INTO MODULES VALUES(4, 'SUB MODULE 1-4', 1);
INSERT INTO MODULES VALUES(5, 'SUB MODULE 1-2-5', 2);
INSERT INTO MODULES VALUES(6, 'SUB MODULE 1-2-5-6', 5);
查询 1:
select t.mod_id,
t.name,
t.prnt_mod_id,
max(LEVEL) as mx_lvl,
SYS_CONNECT_BY_PATH(t.name, '>') as PATH
from MODULES t
start with t.mod_id = 1
connect by NOCYCLE prior t.mod_id = t.prnt_mod_id
group by t.mod_id, t.name, t.prnt_mod_id
order by mx_lvl, t.name
| MOD_ID | NAME | PRNT_MOD_ID | MX_LVL | PATH |
|--------|--------------------|-------------|--------|--------|
| 1 | BASE MODULE | 1 | 1 | (null) |
| 2 | SUB MODULE 1-2 | 1 | 2 | (null) |
| 3 | SUB MODULE 1-3 | 1 | 2 | (null) |
| 4 | SUB MODULE 1-4 | 1 | 2 | (null) |
| 5 | SUB MODULE 1-2-5 | 2 | 3 | (null) |
| 6 | SUB MODULE 1-2-5-6 | 5 | 4 | (null) |
我正在尝试使用 SYS_CONNECT_BY_PATH,如 oracle 文档中所示:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries003.htm#SQLRF52318
http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions171.htm#SQLRF06116
我真的很想显示我的每个模块的分隔路径。
If you specify [a hierarchical query] clause, then do not specify either ORDER BY or GROUP BY, because 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.
(如果确实需要分组,则在前面或后面的子查询中执行,但不要在同一个子查询中执行。)
要获得非空路径的输出,您可以这样做:
查询 1:
select t.mod_id,
t.name,
t.prnt_mod_id,
LEVEL as mx_lvl,
SYS_CONNECT_BY_PATH(t.name, '>') as PATH
from MODULES t
start with t.mod_id = 1
connect by NOCYCLE prior t.mod_id = t.prnt_mod_id
order by mx_lvl, t.name
| MOD_ID | NAME | PRNT_MOD_ID | MX_LVL | PATH |
|--------|--------------------|-------------|--------|-----------------------------------------------------------------|
| 1 | BASE MODULE | 1 | 1 | >BASE MODULE |
| 2 | SUB MODULE 1-2 | 1 | 2 | >BASE MODULE>SUB MODULE 1-2 |
| 3 | SUB MODULE 1-3 | 1 | 2 | >BASE MODULE>SUB MODULE 1-3 |
| 4 | SUB MODULE 1-4 | 1 | 2 | >BASE MODULE>SUB MODULE 1-4 |
| 5 | SUB MODULE 1-2-5 | 2 | 3 | >BASE MODULE>SUB MODULE 1-2>SUB MODULE 1-2-5 |
| 6 | SUB MODULE 1-2-5-6 | 5 | 4 | >BASE MODULE>SUB MODULE 1-2>SUB MODULE 1-2-5>SUB MODULE 1-2-5-6 |
但是,您可能希望将其用作排序子句:
ORDER SIBLINGS BY t.name;
为什么我的 PATH 列充满了空值?分层数据中的循环是否会导致 SYS_CONNECT_BY_PATH 函数出现问题?
Oracle 11g R2 架构设置:
CREATE TABLE MODULES
(
MOD_ID INTEGER NOT NULL,
NAME VARCHAR2(20) NOT NULL,
PRNT_MOD_ID INTEGER
);
INSERT INTO MODULES VALUES(1, 'BASE MODULE', 1);
INSERT INTO MODULES VALUES(2, 'SUB MODULE 1-2', 1);
INSERT INTO MODULES VALUES(3, 'SUB MODULE 1-3', 1);
INSERT INTO MODULES VALUES(4, 'SUB MODULE 1-4', 1);
INSERT INTO MODULES VALUES(5, 'SUB MODULE 1-2-5', 2);
INSERT INTO MODULES VALUES(6, 'SUB MODULE 1-2-5-6', 5);
查询 1:
select t.mod_id,
t.name,
t.prnt_mod_id,
max(LEVEL) as mx_lvl,
SYS_CONNECT_BY_PATH(t.name, '>') as PATH
from MODULES t
start with t.mod_id = 1
connect by NOCYCLE prior t.mod_id = t.prnt_mod_id
group by t.mod_id, t.name, t.prnt_mod_id
order by mx_lvl, t.name
| MOD_ID | NAME | PRNT_MOD_ID | MX_LVL | PATH |
|--------|--------------------|-------------|--------|--------|
| 1 | BASE MODULE | 1 | 1 | (null) |
| 2 | SUB MODULE 1-2 | 1 | 2 | (null) |
| 3 | SUB MODULE 1-3 | 1 | 2 | (null) |
| 4 | SUB MODULE 1-4 | 1 | 2 | (null) |
| 5 | SUB MODULE 1-2-5 | 2 | 3 | (null) |
| 6 | SUB MODULE 1-2-5-6 | 5 | 4 | (null) |
我正在尝试使用 SYS_CONNECT_BY_PATH,如 oracle 文档中所示:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries003.htm#SQLRF52318 http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions171.htm#SQLRF06116
我真的很想显示我的每个模块的分隔路径。
If you specify [a hierarchical query] clause, then do not specify either ORDER BY or GROUP BY, because 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.
(如果确实需要分组,则在前面或后面的子查询中执行,但不要在同一个子查询中执行。)
要获得非空路径的输出,您可以这样做:
查询 1:
select t.mod_id,
t.name,
t.prnt_mod_id,
LEVEL as mx_lvl,
SYS_CONNECT_BY_PATH(t.name, '>') as PATH
from MODULES t
start with t.mod_id = 1
connect by NOCYCLE prior t.mod_id = t.prnt_mod_id
order by mx_lvl, t.name
| MOD_ID | NAME | PRNT_MOD_ID | MX_LVL | PATH |
|--------|--------------------|-------------|--------|-----------------------------------------------------------------|
| 1 | BASE MODULE | 1 | 1 | >BASE MODULE |
| 2 | SUB MODULE 1-2 | 1 | 2 | >BASE MODULE>SUB MODULE 1-2 |
| 3 | SUB MODULE 1-3 | 1 | 2 | >BASE MODULE>SUB MODULE 1-3 |
| 4 | SUB MODULE 1-4 | 1 | 2 | >BASE MODULE>SUB MODULE 1-4 |
| 5 | SUB MODULE 1-2-5 | 2 | 3 | >BASE MODULE>SUB MODULE 1-2>SUB MODULE 1-2-5 |
| 6 | SUB MODULE 1-2-5-6 | 5 | 4 | >BASE MODULE>SUB MODULE 1-2>SUB MODULE 1-2-5>SUB MODULE 1-2-5-6 |
但是,您可能希望将其用作排序子句:
ORDER SIBLINGS BY t.name;