我已尝试过多种方法,但无法获得正确的结果集。 我不愿意在代码和处理中恢复处理这个问题。进行多次查询,因为我觉得那里有一个相当简单的解决方案。
包含流程信息的2个表。
过程:
ID (key)
PARENT_ID
ROOT_ID
过程数据
ID (key)
PROCESS_ID (key in process)
type
col1
col2
我的数据是这样的;
过程
id, root_id, parent_id
100, 1, 10
101, 1, 10
102, 1, 10
10, 1, 1
过程数据
id,process_id,type,col1, col2
201,100,X,NULL,NULL
202,100,X,NULL,NULL
203,100,TYPE1,My Data for Type1,NULL
301,100,X,NULL,NULL
302,100,X,NULL,NULL
303,100,TYPE1,My Data for Type1,NULL
401,100,X,NULL,NULL
402,100,X,NULL,NULL
403,100,TYPE1,My Data for Type1,NULL
503,10,X,NULL,NULL
503,10,TYPE2,NULL,My data for type2
期望的结果
root_id,parent_id,process_id,col1,col2
1,100,203,My data for type1,My data for type2
1,101,303,My data for type1,My data for type2
1,102,403,My data for type1,My data for type2
您可以将其视为流程层次结构;
ROOT PROCESS
TOP_LEVEL PROCESS type=TYPE2 (has a label that applies to all it's children)
(it's possible that between the type1 and type2 header above there is another process_id -> parent_id iteration needed - can ignore for now)
PROCESS type=type1 (has data about this instance of the process)
PROCESS type=type1 (has data about this instance of the process)
PROCESS type=type1 (has data about this instance of the process)
TOP_LEVEL PROCESS type=TYPE2 (has a label that applies to all it's children)
PROCESS type=type1 (has data about this instance of the process)
PROCESS type=type1 (has data about this instance of the process)
PROCESS type=type1 (has data about this instance of the process)
我最接近的是;
with processes as ( -- this gets me all the process_id's for the root process - there will be lots of these with different types
select id
,parent_id
from process as p
where root_process_id='1'
), processdatainfo as ( -- this should get the type 1 child processes
select pd.id
,pd.process_id
,col1
from processdata as pd
where pd.process_id in ( select id from processes )
and pd.type = 'TYPE1'
), parentprocesses as ( -- this should get the type 2 parents that have the label for the children
select process_id
,col2
from db2inst1.processlog
where process_id in ( select id from processes )
and type = 'TYPE2'
)
select * -- and the magic.....well doesn't happen....
from processes
inner join processdatainfo
on (processes.id = processdatainfo.process_id)
left join parentprocesses
on (parentprocesses.process_id = processes.parent_id)
在我的结果中,col2始终为NULL 虽然这是我猜的一件事,但我只获得了我预期的3行。 我已经使用子查询使用嵌套选择来回转,但这种方法似乎最接近。 我已经查看了递归查询,但这个问题似乎并不合适,因为虽然我确实需要处理树,但我需要为结果获取一行而不是每行递归一行。
同样递归看起来很困难 - 它需要在到达type2进程时停止递归,而不是一直到root_id 我无法使用存储过程,因为在这种情况下我无法更新服务器 - 我的应用程序是它的客户端。
虽然表格很大 - 介于60M到140M之间,但对根进程ID进行过滤会将process_id列表降低到10-20,这就是为什么我认为上述WITH会有所帮助。
我认为我需要的是;
听起来很简单......