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

时间:2015-08-19 07:25:38

标签: sql excel oracle odbc

如果记录与当前记录中的某些条件匹配,我需要执行单个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|
-------------------------------------------------------------------------------------------

基本上,jim向james发送请求,但bob批准或拒绝了整个过程中的每一步。这里有3个提交,所以我只需要3条记录,但有些数据来自不同记录的字段中的数据。目前jim获得1次提交和0次拒绝,james获得2次提交和1次拒绝。在这里:如果bob拒绝james提交,请求可以重新分配给sally,并且持有此数据的系统追溯性地将sally称为第4步中的nextUser,这将使得它获得拒绝,但它詹姆斯是错误提交的。在这种情况下,吉姆,詹姆斯和莎莉都得到1提交,但詹姆斯有1拒绝。这就是我需要它输出的内容(最后2个filds" 1' s"是报告数量的提交和拒绝的反制标志)

--------------------------------------------------------------------------------------
|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      |       |
--------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

我已经猜测了一些逻辑(拒绝的行总是会以&#34开头的评论;拒绝:"?此外,拒绝行的操作是否真的是& #34;已接受"?我还做了一些假设,对于提交的行," nextuser"是qa用户;如果不是&n,您可能需要添加一些其他的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