繁体   English   中英

Oracle根据当前记录在另一个记录中选择字段

[英]Oracle select field in another record based on current record

我需要执行一个单独的oracle SQL查询,如果该记录与当前记录中的某些条件匹配,则该查询将从其他记录中返回字段的值。 通过ODBC连接将这些数据从Oracle SQL DB拉入excel以生成报告。 目前,我从表中提取了所有数据并在excel中对其进行了处理,但是记录的数量已增加到某种程度,以至于这不再是可行的选择。 (大约1/4百万)

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个记录,但是一些数据来自不同记录的字段中的数据。 目前jim获得1个提交和0个拒绝,而james得到2个提交和1个拒绝。 这里的注意事项:如果bob拒绝james提交,则可以将请求重新分配给sally,并且将保存此数据的系统作为步骤4中的nextUser sally追溯到sally,这将使sally出现sally遭到拒绝,但提交的是james错误。 在这种情况下,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           

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM