我的系统中有一个工作流程,其中包含以下格式的数据。
From To
1 2
1 3
2 4
3 5
4 5
5 6
6 7
6 8
7 9
8 9
我需要将其转换为以下格式。
S1 S2 S3 S4 S5 S6 S7
1 2 4 5 6 7 9
1 3 5 6 7 9
1 3 5 6 8 9
1 2 4 5 6 8 9
或
S1 S2 S3 S4 S5 S6 S7
1 2 4 5 6 7 9
1 3 5 6 7 9
6 8 9
6 8 9
或任何其他有用的格式
输出表示工作流程过程中可以遵循的可能路径数。
你可以假设我有第一阶段和最后阶段的信息。在这种情况下,您可以假设1 = first
阶段和9 = final
。
因此,一旦用户在Stage 1
,他就可以选择是Stage 2
还是Stage 3
答案 0 :(得分:3)
您需要分层查询,如下所示:
select path
from (select stage_to end,
sys_connect_by_path(stage_from, ' => ') || ' => ' || stage_to path
from test
start with stage_from = 1
connect by stage_from = prior stage_to)
where end = 9;
如果可以接受一列中的输出,请使用sys_connect_by_path
,但如果您需要未知数量的输出列,则可以尝试此问题的任何答案:Dynamic pivot in oracle sql。
测试数据和输出:
with test (stage_from, stage_to) as (
select 1, 2 from dual union all
select 1, 3 from dual union all
select 2, 4 from dual union all
select 3, 5 from dual union all
select 4, 5 from dual union all
select 5, 6 from dual union all
select 6, 7 from dual union all
select 6, 8 from dual union all
select 7, 9 from dual union all
select 8, 9 from dual)
select path
from (select stage_to end,
sys_connect_by_path(stage_from, ' => ') || ' => ' || stage_to path
from test
start with stage_from = 1
connect by stage_from = prior stage_to)
where end = 9;
输出:
PATH
---------------------------------------
=> 1 => 2 => 4 => 5 => 6 => 7 => 9
=> 1 => 2 => 4 => 5 => 6 => 8 => 9
=> 1 => 3 => 5 => 6 => 7 => 9
=> 1 => 3 => 5 => 6 => 8 => 9
答案 1 :(得分:2)
好的,所以我遇到的主要问题是'1'只出现在'From'列中,而'9'只出现在'To'列中。为了解决这个问题,我在这个设置的数据中增加了一行。显然,这可能在现实世界中不起作用。
create table temp1 as
select 1 as frm, 2 as too from dual
union all
select 1, 3 from dual
union all
select 2,4 from dual
union all
select 3,5 from dual
union all
select 4,5 from dual
union all
select 5,6 from dual
union all
select 6,7 from dual
union all
select 6,8 from dual
union all
select 7,9 from dual
union all
select 8,9 from dual
union all
select 9, 0 from dual
然后有这个(非常混乱的)代码。
select
t1a.frm as s1,
t1b.frm as s2,
t1c.frm as s3,
t1d.frm as s4,
t1e.frm as s5,
t1f.frm as s6,
t1g.frm as s7,
t1h.frm as s8
from
temp1 t1a
left join
temp1 t1b
on T1A.too = t1b.frm
left join
temp1 t1c
on t1b.too = t1c.frm
left join
temp1 t1d
on t1c.too = t1d.frm
left join
temp1 t1e
on t1d.too = t1e.frm
left join
temp1 t1f
on t1e.too = t1f.frm
left join
temp1 t1g
on t1f.too = t1g.frm
left join
temp1 t1h
on t1g.too = t1h.frm
where t1a.frm = 1
给出输出
s1 s2 s3 s4 s5 s6 s7 s8
1 3 5 6 8 9
1 3 5 6 7 9
1 2 4 5 6 7 9
1 2 4 5 6 8 9
不确定这是否正是您所寻找的,但希望它能为您提供一些想法