我在Oracle 10g中执行以下查询以检索分层形式的数据(完整查询有点复杂):
SELECT LEVEL AS lvl, a.*
FROM (
WITH temp
AS (...)
SELECT id_request, subj_type, id_subj, name, flag_e,
person_code, assoc_type, nature, parent
FROM temp,
(...)
) a
START WITH a.parent IS NULL
CONNECT BY PRIOR a.id_subj = a.parent;
这是内部选择返回的集合:
ID_REQUEST SUBJ_TYPE ID_SUBJ NAME FLAG_E PERSON_CODE ASSOC_TYPE NATURE PARENT
91948 F 4A4BE76C44D4003CE0530AA000A6003C John Smith 0 xxxyyy123456zzzzz Declarant F NULL
91948 C 4A4BE76C44D6003CE0530AA000A6003C Rose Anderson 0 kkkkkk654321qqqqq NULL F 4A4BE76C44D4003CE0530AA000A6003C
如果我运行整个查询没有 START WITH子句,我会正确地得到以下结果:
LVL ID_REQUEST SUBJ_TYPE ID_SUBJ NAME FLAG_E PERSON_CODE ASSOC_TYPE NATURE PARENT
1 91948 C 4A4BE76C44D6003CE0530AA000A6003C Rose Anderson 0 kkkkkk654321qqqqq NULL F 4A4BE76C44D4003CE0530AA000A6003C
1 91948 F 4A4BE76C44D4003CE0530AA000A6003C John Smith 0 xxxyyy123456zzzzz Declarant F NULL
2 91948 C 4A4BE76C44D6003CE0530AA000A6003C Rose Anderson 0 kkkkkk654321qqqqq NULL F 4A4BE76C44D4003CE0530AA000A6003C
但如果我使用
LVL ID_REQUEST SUBJ_TYPE ID_SUBJ NAME FLAG_E PERSON_CODE ASSOC_TYPE NATURE PARENT
1 91948 F 4A4BE76C44D4003CE0530AA000A6003C John Smith 0 xxxyyy123456zzzzz Declarant F NULL
2 91948 C 4A4BE76C44D6003CE0530AA000A6003C Rose Anderson 0 kkkkkk654321qqqqq NULL F 4A4BE76C44D4003CE0530AA000A6003C
最奇怪的事情是:
这是使用创建的表的查询:
select LEVEL as lvl, a.*
from (select * from test_tbl) a
start with a.parent is null
connect by PRIOR a.id_subj = a.parent;
似乎START WITH子句与PARENT字段中的NULL值不匹配。为什么会这样?
提前致谢。 最好的问候。
答案 0 :(得分:1)
请尝试以下方法。 " Materialise的"提示很重要。
WITH
temp AS (...),
a as (
SELECT /*+ materialize */ id_request, subj_type, id_subj, name, flag_e,
person_code, assoc_type, nature, parent
FROM temp,
(...)
)
SELECT LEVEL AS lvl, a.*
FROM a
START WITH a.parent IS NULL
CONNECT BY PRIOR a.id_subj = a.parent;