0

这是我用来生成表的查询:

SELECT cu.idchanneluser AS Approver,
     mcr.idrule AS Rule,
     mrd.idseq AS Seq,
     mcr.idcust AS CustID,
     cu.iduser AS USERID
FROM mstchanneluser cu,
     mstcatruledetail mrd,
     mstcatrule mcr
WHERE idchannel='01'
     AND mrd.idlist=cu.iduser
     AND mrd.idrule=mcr.idrule
     AND mcr.nbrauth='2'
     AND mcr.isautoauth='N'

这是结果的快照:

在此处输入图像描述

但我想要这样的结果:

   FirstAuthorizer SecondAuthorzier Rule    
   rohitcorp        ajitcorp         3090  
   CORPTEST         TESTCORP         8634
   ABHIMAKER        CORPTEST         11705

我试过这个查询:

SELECT CASE WHEN idseq = '0' THEN idchanneluser ELSE NULL END AS Approver,
    case when idseq = '1' THEN idchanneluser  ELSE NULL END AS secondApprover,
    cu.idchanneluser AS Approver,
    mcr.idrule AS Rule,
    mrd.idseq AS Seq,
    mcr.idcust AS CustID,
    cu.iduser AS USERID
FROM mstchanneluser cu, 
    mstcatruledetail mrd, 
    mstcatrule mcr 
WHERE idchannel='01'
    AND mrd.idlist=cu.iduser
    AND mrd.idrule=mcr.idrule 
    AND mcr.nbrauth='2'
    AND mcr.isautoauth='N'

它会返回我这个结果检查快照。

在此处输入图像描述

4

2 回答 2

3

这是从您的第二个查询派生的,您只需要使用MAX它们并将它们分组mcr.idrule

SELECT MAX(CASE WHEN idseq = '0' THEN idchanneluser ELSE NULL END) AS Approver
    ,  MAX(CASE WHEN idseq = '1' THEN idchanneluser ELSE NULL END) AS secondApprover
    ,  mcr.idrule AS RULE
FROM   mstchanneluser cu
    ,  mstcatruledetail mrd
    ,  mstcatrule mcr
WHERE idchannel = '01'
    AND mrd.idlist = cu.iduser
    AND mrd.idrule = mcr.idrule
    AND mcr.nbrauth = '2'
    AND mcr.isautoauth = 'N'
GROUP BY mcr.idrule
于 2012-11-23T04:48:30.837 回答
0

oracle 中的另一种方法,具有分析功能超前滞后

select tab.y FirstAuthorizer,tab.x SecondAuthorzier,tab.rule from
(
select lead(appprover,0) over (partition by rule order by seq) x ,
lag(appprover,1) over (partition by rule order by seq) y,
rule
from tbl ) tab
where tab.y is not null;
于 2012-11-23T10:46:54.560 回答