Oracle根据当前loggingselect另一条logging中的字段

我需要执行一个单一的oracle SQL查询,如果logging与当前logging中的特定条件匹配,将返回不同logging中某个字段的值。 通过ODBC连接将这些数据从Oracle SQL DB中提取到excel中以生成报告。 目前我从表格中提取所有的数据并在excel中进行处理,但是logging的数量已经增加到不再是可行的选项。 (约一千四百万)

SOURCE TABLE ------------------------------------------------------------------------------------------- |major |minor |step |currentUser |NextUser |comment |stage |action |timestamp | ------------------------------------------------------------------------------------------- |475 |13 |1 |jim |bob |request created |QA |submit |12-19-2005| |475 |13 |2 |bob |james |request approved |RA |accept |12-20-2005| |475 |13 |3 |james |bob |data submitted |QA |submit |12-21-2005| |475 |13 |4 | |james |rejected: thisISwhy|RA |accept |12-22-2005| |475 |13 |5 |James |bob |data submitted |QA |submit |12-23-2005| |475 |13 |6 | |jim |data approved |SC |complete|12-24-2005| |475 |13 |6 | | |request closed |SC |closed |12-24-2005| ------------------------------------------------------------------------------------------- 

基本上,吉姆发送一个请求詹姆斯,但鲍勃批准或拒绝的每一步。 这里有3个提交,所以我只需要3条logging,但是一些数据来自不同logging的字段中的数据。 目前吉姆得到1提交和0拒绝和詹姆斯得到2提交和1拒绝。 这里的捕捉:如果鲍勃拒绝詹姆斯提交,请求可以被重新分配到sally和拥有这个数据的系统追溯性地作为下一个用户在步骤4,这将使得它APPEAR,sally得到了拒绝sally,但它是詹姆斯谁提交错误。 在这种情况下,jim,james和sally全部得到1,但是james有1个被拒绝。 这是我需要它输出(最后2个“1”是报告提交和拒绝次数的计数器标志)

 -------------------------------------------------------------------------------------- |major |minor |step |submiter |QA_rep|comment |timestamp |submit |reject | -------------------------------------------------------------------------------------- |475 |13 |1 |jim |bob |thing created |12-19-2005 |1 | | |475 |13 |3 |james |bob |rejected: thisISwhy|12-22-2005 |1 |1 | |475 |13 |5 |james |bob |data approved |12-22-2005 |1 | | -------------------------------------------------------------------------------------- 

我猜测了一些逻辑(被拒绝的行总是有一个以“rejected:”开始的注释吗?另外,被拒绝行的行为是否真的被“接受”?我也做了一些假设对于提交的行,“nextuser”是qa用户;如果情况并非总是如此,那么您可能需要添加一些额外的case语句我已经猜测了您的情况,行被拒绝并分配给其他人。

如果我的任何假设是不正确的,希望你能够修改我的查询,以适应你的目的。

 with source_table as (select 475 major, 13 minor, 1 step, 'jim' currentuser, 'bob' nextuser, 'request created' request_comment, 'QA' stage, 'submit' action, to_date('19/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 13 minor, 2 step, 'bob' currentuser, 'james' nextuser, 'request approved' request_comment, 'RA' stage, 'accept' action, to_date('20/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 13 minor, 3 step, 'james' currentuser, 'bob' nextuser, 'data submitted' request_comment, 'QA' stage, 'submit' action, to_date('21/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 13 minor, 4 step, null currentuser, 'james' nextuser, 'rejected: thisISwhy' request_comment, 'RA' stage, 'accept' action, to_date('22/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 13 minor, 5 step, 'james' currentuser, 'bob' nextuser, 'data submitted' request_comment, 'QA' stage, 'submit' action, to_date('23/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 13 minor, 6 step, null currentuser, 'jim' nextuser, 'data approved' request_comment, 'SC' stage, 'complete' action, to_date('24/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 13 minor, 6 step, null currentuser, null nextuser, 'request closed' request_comment, 'SC' stage, 'closed' action, to_date('24/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 14 minor, 1 step, 'jim' currentuser, 'bob' nextuser, 'request created' request_comment, 'QA' stage, 'submit' action, to_date('19/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 14 minor, 2 step, 'bob' currentuser, 'james' nextuser, 'request approved' request_comment, 'RA' stage, 'accept' action, to_date('20/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 14 minor, 3 step, 'james' currentuser, 'bob' nextuser, 'data submitted' request_comment, 'QA' stage, 'submit' action, to_date('21/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 14 minor, 4 step, null currentuser, 'sally' nextuser, 'rejected: thisISwhy' request_comment, 'RA' stage, 'accept' action, to_date('22/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 14 minor, 5 step, 'sally' currentuser, 'bob' nextuser, 'data submitted' request_comment, 'QA' stage, 'submit' action, to_date('23/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 14 minor, 6 step, null currentuser, 'jim' nextuser, 'data approved' request_comment, 'SC' stage, 'complete' action, to_date('24/12/2005', 'dd/mm/yyyy') dt from dual union all select 475 major, 14 minor, 6 step, null currentuser, null nextuser, 'request closed' request_comment, 'SC' stage, 'closed' action, to_date('24/12/2005', 'dd/mm/yyyy') dt from dual), res as (select major, minor, step, currentuser, nextuser, request_comment, dt, stage, action, lead(request_comment) over (partition by major, minor order by step) next_comment, case when lead(request_comment) over (partition by major, minor order by step) like 'rejected:%' then 1 end rejected from source_table) select major, minor, step, currentuser submitter, nextuser qa_rep, next_comment request_comment, dt, 1 submit, rejected from res where action = 'submit'; MAJOR MINOR STEP SUBMITTER QA_REP REQUEST_COMMENT DT SUBMIT REJECTED ---------- ---------- ---------- --------- ------ ------------------- ---------- ---------- ---------- 475 13 1 jim bob request approved 19/12/2005 1 475 13 3 james bob rejected: thisISwhy 21/12/2005 1 1 475 13 5 james bob data approved 23/12/2005 1 475 14 1 jim bob request approved 19/12/2005 1 475 14 3 james bob rejected: thisISwhy 21/12/2005 1 1 475 14 5 sally bob data approved 23/12/2005 1