2012-11-23 67 views
0

這是我用來生成我的表的查詢。sql選擇顯示行數據作爲varchar項的列

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' 

和結果enter image description here

這個快照,但我想造成這樣

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' 

而且將回到我這個結果檢查快照。

enter image description here

回答

3

這是從你的第二個查詢得到的,你只需要通過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 
+1

非常感謝你@kuya約翰它開始爲我工作。謝謝 –

+0

不客氣,順便說一下,什麼數據庫您正在使用的服務器? –

+0

Oracle數據庫和我們正在使用PL/SQL的客戶端 –

0

在Oracle使用MAX和他們組分析功能超前滯後的另一種方式

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;