【问题标题】:Oracle select field in another record based on current recordOracle根据当前记录选择另一条记录中的字段
【发布时间】:2015-11-12 08:54:06
【问题描述】:

如果记录与当前记录中的某些条件匹配,我需要执行单个 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,并且保存此数据的系统在步骤 4 中追溯地将 sally 指定为 nextUser,这将使 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      |       |
--------------------------------------------------------------------------------------

【问题讨论】:

  • 如果您可以更新您的示例输入和输出数据以演示将被拒绝的提交分配给其他人的情况,这将很有帮助。

标签: sql excel oracle odbc


【解决方案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           

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多