这是我构建在oracle bi中使用的分层表的查询 实际上我在我的学科领域使用它。 但有时当我向下钻取值标签时,我的分析显示没有结果但是当我使用标题标签钻取时它工作得很好!!!! 当我过滤水平时,它的显示没有结果 你能不能帮助我。
with kol_power as
(select level lvl,
p.id,
p.parameter_name title,
p.parent_system_parameter_id parentid,
p.ip hierarchicode,
system_id
from EVAL_sys_paramter p
connect by prior id = parent_system_parameter_id
start with parent_system_parameter_id is null),
l1 as
(select * from kol_power where lvl = 1),
l2 as
(select *
from kol_power k
where k.lvl = 2
and (parentid, system_id) in (select id, system_id from l1)),
l3 as
(select *
from kol_power k
where k.lvl = 3
and (parentid, system_id) in (select id, system_id from l2)),
l4 as
(select *
from kol_power k
where k.lvl = 4
and (parentid, system_id) in (select id, system_id from l3)),
l5 as
(select *
from kol_power k
where k.lvl = 5
and (parentid, system_id) in (select id, system_id from l4)),
l6 as
(select *
from kol_power k
where k.lvl = 6
and (parentid, system_id) in (select id, system_id from l5)),
l7 as
(select *
from kol_power k
where k.lvl = 7
and (parentid, system_id) in (select id, system_id from l6)),
l8 as
(select *
from kol_power k
where k.lvl = 8
and (parentid, system_id) in (select id, system_id from l7)),
l9 as
(select *
from kol_power k
where k.lvl = 9
and (parentid, system_id) in (select id, system_id from l8)),
r9 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
l3.id l3_id,
l3.title l3_title,
l3.hierarchicode l3_hierarchicode,
l4.id l4_id,
l4.title l4_title,
l4.hierarchicode l4_hierarchicode,
l5.id l5_id,
l5.title l5_title,
l5.hierarchicode l5_hierarchicode,
l6.id l6_id,
l6.title l6_title,
l6.hierarchicode l6_hierarchicode,
l7.id l7_id,
l7.title l7_title,
l7.hierarchicode l7_hierarchicode,
l8.id l8_id,
l8.title l8_title,
l8.hierarchicode l8_hierarchicode,
l9.id l9_id,
l9.title l9_title,
l9.hierarchicode l9_hierarchicode,
l2.system_id
from l1, l2, l3, l4, l5, l6, l7, l8, l9
where l2.parentid = l1.id
and l2.system_id = l1.system_id
and l3.parentid = l2.id
and l3.system_id = l2.system_id
and l4.parentid = l3.id
and l4.system_id = l3.system_id
and l5.parentid = l4.id
and l5.system_id = l4.system_id
and l6.parentid = l5.id
and l6.system_id = l5.system_id
and l7.parentid = l6.id
and l7.system_id = l6.system_id
and l8.parentid = l7.id
and l8.system_id = l7.system_id
and l9.parentid = l8.id
and l9.system_id = l8.system_id
),
r8 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
l3.id l3_id,
l3.title l3_title,
l3.hierarchicode l3_hierarchicode,
l4.id l4_id,
l4.title l4_title,
l4.hierarchicode l4_hierarchicode,
l5.id l5_id,
l5.title l5_title,
l5.hierarchicode l5_hierarchicode,
l6.id l6_id,
l6.title l6_title,
l6.hierarchicode l6_hierarchicode,
l7.id l7_id,
l7.title l7_title,
l7.hierarchicode l7_hierarchicode,
l8.id l8_id,
l8.title l8_title,
l8.hierarchicode l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l2.system_id
from l1, l2, l3, l4, l5, l6, l7, l8
where l2.parentid = l1.id
and l2.system_id = l1.system_id
and l3.parentid = l2.id
and l3.system_id = l2.system_id
and l4.parentid = l3.id
and l4.system_id = l3.system_id
and l5.parentid = l4.id
and l5.system_id = l4.system_id
and l6.parentid = l5.id
and l6.system_id = l5.system_id
and l7.parentid = l6.id
and l7.system_id = l6.system_id
and l8.parentid = l7.id
and l8.system_id = l7.system_id),
r7 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
l3.id l3_id,
l3.title l3_title,
l3.hierarchicode l3_hierarchicode,
l4.id l4_id,
l4.title l4_title,
l4.hierarchicode l4_hierarchicode,
l5.id l5_id,
l5.title l5_title,
l5.hierarchicode l5_hierarchicode,
l6.id l6_id,
l6.title l6_title,
l6.hierarchicode l6_hierarchicode,
l7.id l7_id,
l7.title l7_title,
l7.hierarchicode l7_hierarchicode,
null l8_id,
null l8_title,
null l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l2.system_id
from l1, l2, l3, l4, l5, l6, l7
where l2.parentid = l1.id
and l2.system_id = l1.system_id
and l3.parentid = l2.id
and l3.system_id = l2.system_id
and l4.parentid = l3.id
and l4.system_id = l3.system_id
and l5.parentid = l4.id
and l5.system_id = l4.system_id
and l6.parentid = l5.id
and l6.system_id = l5.system_id
and l7.parentid = l6.id
and l7.system_id = l6.system_id),
r6 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
l3.id l3_id,
l3.title l3_title,
l3.hierarchicode l3_hierarchicode,
l4.id l4_id,
l4.title l4_title,
l4.hierarchicode l4_hierarchicode,
l5.id l5_id,
l5.title l5_title,
l5.hierarchicode l5_hierarchicode,
l6.id l6_id,
l6.title l6_title,
l6.hierarchicode l6_hierarchicode,
null l7_id,
null l7_title,
null l7_hierarchicode,
null l8_id,
null l8_title,
null l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l2.system_id
from l1, l2, l3, l4, l5, l6
where l2.parentid = l1.id
and l2.system_id = l1.system_id
and l3.parentid = l2.id
and l3.system_id = l2.system_id
and l4.parentid = l3.id
and l4.system_id = l3.system_id
and l5.parentid = l4.id
and l5.system_id = l4.system_id
and l6.parentid = l5.id
and l6.system_id = l5.system_id)
,
r5 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
l3.id l3_id,
l3.title l3_title,
l3.hierarchicode l3_hierarchicode,
l4.id l4_id,
l4.title l4_title,
l4.hierarchicode l4_hierarchicode,
l5.id l5_id,
l5.title l5_title,
l5.hierarchicode l5_hierarchicode,
null l6_id,
null l6_title,
null l6_hierarchicode,
null l7_id,
null l7_title,
null l7_hierarchicode,
null l8_id,
null l8_title,
null l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l2.system_id
from l1, l2, l3, l4, l5
where l2.parentid = l1.id
and l2.system_id = l1.system_id
and l3.parentid = l2.id
and l3.system_id = l2.system_id
and l4.parentid = l3.id
and l4.system_id = l3.system_id
and l5.parentid = l4.id
and l5.system_id = l4.system_id)
,
r4 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
l3.id l3_id,
l3.title l3_title,
l3.hierarchicode l3_hierarchicode,
l4.id l4_id,
l4.title l4_title,
l4.hierarchicode l4_hierarchicode,
null l5_id,
null l5_title,
null l5_hierarchicode,
null l6_id,
null l6_title,
null l6_hierarchicode,
null l7_id,
null l7_title,
null l7_hierarchicode,
null l8_id,
null l8_title,
null l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l2.system_id
from l1, l2, l3, l4
where l2.parentid = l1.id
and l2.system_id = l1.system_id
and l3.parentid = l2.id
and l3.system_id = l2.system_id
and l4.parentid = l3.id
and l4.system_id = l3.system_id)
,
r3 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
l3.id l3_id,
l3.title l3_title,
l3.hierarchicode l3_hierarchicode,
null l4_id,
null l4_title,
null l4_hierarchicode,
null l5_id,
null l5_title,
null l5_hierarchicode,
null l6_id,
null l6_title,
null l6_hierarchicode,
null l7_id,
null l7_title,
null l7_hierarchicode,
null l8_id,
null l8_title,
null l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l2.system_id
from l1, l2, l3
where l2.parentid = l1.id
and l2.system_id = l1.system_id
and l3.parentid = l2.id
and l3.system_id = l2.system_id)
,
r2 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
l2.id l2_id,
l2.title l2_title,
l2.hierarchicode l2_hierarchicode,
null l3_id,
null l3_title,
null l3_hierarchicode,
null l4_id,
null l4_title,
null l4_hierarchicode,
null l5_id,
null l5_title,
null l5_hierarchicode,
null l6_id,
null l6_title,
null l6_hierarchicode,
null l7_id,
null l7_title,
null l7_hierarchicode,
null l8_id,
null l8_title,
null l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l2.system_id
from l1, l2
where l2.parentid = l1.id
and l2.system_id = l1.system_id)
,
r1 as
(select l1.id l1_id,
l1.title l1_title,
l1.hierarchicode l1_hierarchicode,
null l2_id,
null l2_title,
null l2_hierarchicode,
null l3_id,
null l3_title,
null l3_hierarchicode,
null l4_id,
null l4_title,
null l4_hierarchicode,
null l5_id,
null l5_title,
null l5_hierarchicode,
null l6_id,
null l6_title,
null l6_hierarchicode,
null l7_id,
null l7_title,
null l7_hierarchicode,
null l8_id,
null l8_title,
null l8_hierarchicode,
null l9_id,
null l9_title,
null l9_hierarchicode,
l1.system_id
from l1)
select l9_id ID,
l9_title lvl_title,
l9_hierarchicode hierarchicode,
9 lvl,
r9."L1_ID",
r9."L1_TITLE",
r9."L1_HIERARCHICODE",
r9."L2_ID",
r9."L2_TITLE",
r9."L2_HIERARCHICODE",
r9."L3_ID",
r9."L3_TITLE",
r9."L3_HIERARCHICODE",
r9."L4_ID",
r9."L4_TITLE",
r9."L4_HIERARCHICODE",
r9."L5_ID",
r9."L5_TITLE",
r9."L5_HIERARCHICODE",
r9."L6_ID",
r9."L6_TITLE",
r9."L6_HIERARCHICODE",
r9."L7_ID",
r9."L7_TITLE",
r9."L7_HIERARCHICODE",
r9."L8_ID",
r9."L8_TITLE",
r9."L8_HIERARCHICODE",
r9."L9_ID",
r9."L9_TITLE",
r9."L9_HIERARCHICODE",
r9."SYSTEM_ID"
from r9
union all
select l8_id,
l8_title,
l8_hierarchicode,
8,
r8."L1_ID",
r8."L1_TITLE",
r8."L1_HIERARCHICODE",
r8."L2_ID",
r8."L2_TITLE",
r8."L2_HIERARCHICODE",
r8."L3_ID",
r8."L3_TITLE",
r8."L3_HIERARCHICODE",
r8."L4_ID",
r8."L4_TITLE",
r8."L4_HIERARCHICODE",
r8."L5_ID",
r8."L5_TITLE",
r8."L5_HIERARCHICODE",
r8."L6_ID",
r8."L6_TITLE",
r8."L6_HIERARCHICODE",
r8."L7_ID",
r8."L7_TITLE",
r8."L7_HIERARCHICODE",
r8."L8_ID",
r8."L8_TITLE",
r8."L8_HIERARCHICODE",
r8."L9_ID",
r8."L9_TITLE",
r8."L9_HIERARCHICODE",
r8."SYSTEM_ID"
from r8
union all
select l7_id,
l7_title,
l7_hierarchicode,
7,
r7."L1_ID",
r7."L1_TITLE",
r7."L1_HIERARCHICODE",
r7."L2_ID",
r7."L2_TITLE",
r7."L2_HIERARCHICODE",
r7."L3_ID",
r7."L3_TITLE",
r7."L3_HIERARCHICODE",
r7."L4_ID",
r7."L4_TITLE",
r7."L4_HIERARCHICODE",
r7."L5_ID",
r7."L5_TITLE",
r7."L5_HIERARCHICODE",
r7."L6_ID",
r7."L6_TITLE",
r7."L6_HIERARCHICODE",
r7."L7_ID",
r7."L7_TITLE",
r7."L7_HIERARCHICODE",
r7."L8_ID",
r7."L8_TITLE",
r7."L8_HIERARCHICODE",
r7."L9_ID",
r7."L9_TITLE",
r7."L9_HIERARCHICODE",
r7."SYSTEM_ID"
from r7
union all
select l6_id,
l6_title,
l6_hierarchicode,
6,
r6."L1_ID",
r6."L1_TITLE",
r6."L1_HIERARCHICODE",
r6."L2_ID",
r6."L2_TITLE",
r6."L2_HIERARCHICODE",
r6."L3_ID",
r6."L3_TITLE",
r6."L3_HIERARCHICODE",
r6."L4_ID",
r6."L4_TITLE",
r6."L4_HIERARCHICODE",
r6."L5_ID",
r6."L5_TITLE",
r6."L5_HIERARCHICODE",
r6."L6_ID",
r6."L6_TITLE",
r6."L6_HIERARCHICODE",
r6."L7_ID",
r6."L7_TITLE",
r6."L7_HIERARCHICODE",
r6."L8_ID",
r6."L8_TITLE",
r6."L8_HIERARCHICODE",
r6."L9_ID",
r6."L9_TITLE",
r6."L9_HIERARCHICODE",
r6."SYSTEM_ID"
from r6
union all
select l5_id,
l5_title,
l5_hierarchicode,
5,
r5."L1_ID",
r5."L1_TITLE",
r5."L1_HIERARCHICODE",
r5."L2_ID",
r5."L2_TITLE",
r5."L2_HIERARCHICODE",
r5."L3_ID",
r5."L3_TITLE",
r5."L3_HIERARCHICODE",
r5."L4_ID",
r5."L4_TITLE",
r5."L4_HIERARCHICODE",
r5."L5_ID",
r5."L5_TITLE",
r5."L5_HIERARCHICODE",
r5."L6_ID",
r5."L6_TITLE",
r5."L6_HIERARCHICODE",
r5."L7_ID",
r5."L7_TITLE",
r5."L7_HIERARCHICODE",
r5."L8_ID",
r5."L8_TITLE",
r5."L8_HIERARCHICODE",
r5."L9_ID",
r5."L9_TITLE",
r5."L9_HIERARCHICODE",
r5."SYSTEM_ID"
from r5
union all
select l4_id,
l4_title,
l4_hierarchicode,
4,
r4."L1_ID",
r4."L1_TITLE",
r4."L1_HIERARCHICODE",
r4."L2_ID",
r4."L2_TITLE",
r4."L2_HIERARCHICODE",
r4."L3_ID",
r4."L3_TITLE",
r4."L3_HIERARCHICODE",
r4."L4_ID",
r4."L4_TITLE",
r4."L4_HIERARCHICODE",
r4."L5_ID",
r4."L5_TITLE",
r4."L5_HIERARCHICODE",
r4."L6_ID",
r4."L6_TITLE",
r4."L6_HIERARCHICODE",
r4."L7_ID",
r4."L7_TITLE",
r4."L7_HIERARCHICODE",
r4."L8_ID",
r4."L8_TITLE",
r4."L8_HIERARCHICODE",
r4."L9_ID",
r4."L9_TITLE",
r4."L9_HIERARCHICODE",
r4."SYSTEM_ID"
from r4
union all
select l3_id,
l3_title,
l3_hierarchicode,
3,
r3."L1_ID",
r3."L1_TITLE",
r3."L1_HIERARCHICODE",
r3."L2_ID",
r3."L2_TITLE",
r3."L2_HIERARCHICODE",
r3."L3_ID",
r3."L3_TITLE",
r3."L3_HIERARCHICODE",
r3."L4_ID",
r3."L4_TITLE",
r3."L4_HIERARCHICODE",
r3."L5_ID",
r3."L5_TITLE",
r3."L5_HIERARCHICODE",
r3."L6_ID",
r3."L6_TITLE",
r3."L6_HIERARCHICODE",
r3."L7_ID",
r3."L7_TITLE",
r3."L7_HIERARCHICODE",
r3."L8_ID",
r3."L8_TITLE",
r3."L8_HIERARCHICODE",
r3."L9_ID",
r3."L9_TITLE",
r3."L9_HIERARCHICODE",
r3."SYSTEM_ID"
from r3
union all
select l2_id,
l2_title,
l2_hierarchicode,
2,
r2."L1_ID",
r2."L1_TITLE",
r2."L1_HIERARCHICODE",
r2."L2_ID",
r2."L2_TITLE",
r2."L2_HIERARCHICODE",
r2."L3_ID",
r2."L3_TITLE",
r2."L3_HIERARCHICODE",
r2."L4_ID",
r2."L4_TITLE",
r2."L4_HIERARCHICODE",
r2."L5_ID",
r2."L5_TITLE",
r2."L5_HIERARCHICODE",
r2."L6_ID",
r2."L6_TITLE",
r2."L6_HIERARCHICODE",
r2."L7_ID",
r2."L7_TITLE",
r2."L7_HIERARCHICODE",
r2."L8_ID",
r2."L8_TITLE",
r2."L8_HIERARCHICODE",
r2."L9_ID",
r2."L9_TITLE",
r2."L9_HIERARCHICODE",
r2."SYSTEM_ID"
from r2
union all
select l1_id,
l1_title,
l1_hierarchicode,
1,
r1."L1_ID",
r1."L1_TITLE",
r1."L1_HIERARCHICODE",
r1."L2_ID",
r1."L2_TITLE",
r1."L2_HIERARCHICODE",
r1."L3_ID",
r1."L3_TITLE",
r1."L3_HIERARCHICODE",
r1."L4_ID",
r1."L4_TITLE",
r1."L4_HIERARCHICODE",
r1."L5_ID",
r1."L5_TITLE",
r1."L5_HIERARCHICODE",
r1."L6_ID",
r1."L6_TITLE",
r1."L6_HIERARCHICODE",
r1."L7_ID",
r1."L7_TITLE",
r1."L7_HIERARCHICODE",
r1."L8_ID",
r1."L8_TITLE",
r1."L8_HIERARCHICODE",
r1."L9_ID",
r1."L9_TITLE",
r1."L9_HIERARCHICODE",
r1."SYSTEM_ID"
from r1
/*union all
select -1,'????' , 2,
l1_id l1_id,
'???? ???????? ?????? ??????' l1_title,
-1,'????',
null l3_id,
null l3_title,
null l4_id,
null l4_title,
null l5_id,
null l5_title,
null l6_id,
null l6_title,
null l7_id,
null l7_title,
null l8_id,
null l8_title,
null l9_id,
null l9_title from
r1;*/;
答案 0 :(得分:0)
你做的是某种pivot
。这可以通过递归CTE实现,但您需要Oracle 11或更高版本。请尝试此查询:
with eval_sys_paramter as (
-- sample test data
select 1 id, 'P1' parameter_name, null parent_system_parameter_id,
'255.255.255.1' ip, 'S1' system_id from dual union all
select 11 id, 'P11' parameter_name, 1 parent_system_parameter_id,
'255.255.255.11' ip, 'S1' system_id from dual union all
select 12 id, 'P12' parameter_name, 1 parent_system_parameter_id,
'255.255.255.12' ip, 'S1' system_id from dual union all
select 121 id, 'P121' parameter_name, 12 parent_system_parameter_id,
'255.255.255.121' ip, 'S1' system_id from dual ),
-- your query starts here, uncomment "with"
/* with */ t (lvl, id, ttl, pid, sid,
i1, t1, p1,
i2, t2, p2,
i3, t3, p3 ) as (
select 1, id, parameter_name, ip, system_id,
id i1, parameter_name t1, ip p1,
cast (null as number) i2, null t2, null p2,
cast (null as number) i3, null t3, null p3
from eval_sys_paramter where parent_system_parameter_id is null
union all
select lvl + 1,
e.id, e.parameter_name, e.ip, e.system_id,
-- level 1
t.i1, t.t1, t.p1,
-- level 2
case lvl + 1 when 2 then e.id else t.i2 end,
case lvl + 1 when 2 then e.parameter_name else t.t2 end,
case lvl + 1 when 2 then e.ip else t.p2 end,
-- level 3
case lvl + 1 when 3 then e.id else t.i3 end,
case lvl + 1 when 3 then e.parameter_name else t.t3 end,
case lvl + 1 when 3 then e.ip else t.p3 end
from t join eval_sys_paramter e
on e.system_id = t.sid
and e.parent_system_parameter_id = t.id )
select * from t
为4-9级添加类似的部分。