2015-10-19 245 views
0

Excel Image Reference我有這條SQL語句顯示按部門的審批區域以及返回的工作流審批或拒絕次數。我目前的聲明如下,以及它目前正在製作的內容,接下來是我希望它看起來如何。它與團隊有關,並且按順序排列,但我無法弄清楚如何改變順序以達到我的願望。任何幫助表示讚賞。我附上了一張圖片來展示當前的結果與慾望的結果。按組排序/排序按

SELECT DECODE(SUBSTR(P.DESCR,1,18),'Chartfield Signers','Chartfield Signers',P.DESCR), DECODE(U.EOAWSTEP_STATUS,'A','Approval','Denial'), COUNT(U.EOAWSTEP_INSTANCE), TO_CHAR(AVG(
    EXTRACT(DAY FROM (U.EOAWDTTM_MODIFIED - U.DTTM_CREATED) DAY TO SECOND) + 
    (EXTRACT(HOUR FROM (U.EOAWDTTM_MODIFIED - U.DTTM_CREATED) DAY TO SECOND)/24) + 
    (EXTRACT(MINUTE FROM (U.EOAWDTTM_MODIFIED - U.DTTM_CREATED) DAY TO SECOND)/(60*24)) + 
    (EXTRACT(SECOND FROM (U.EOAWDTTM_MODIFIED - U.DTTM_CREATED) DAY TO SECOND)/(60*60*24))),'999.99') "DURATION IN DAYS" 
    FROM PS_EOAW_STEPINST S , PS_EOAW_STEP P , PS_MDA_EPAF_WFXREF T , PS_EOAW_USERINST U 
    WHERE T.EOAWPRCS_ID LIKE 'MDA_EPAF%' 
    AND T.EOAWTHREAD_ID = S.EOAWTHREAD_ID 
    AND T.EOAWPRCS_ID = S.EOAWPRCS_ID 
    AND S.EOAWSTEP_INSTANCE = U.EOAWSTEP_INSTANCE 
    AND U.EOAWSTEP_STATUS IN ('A','D') 
    AND P.EOAWPRCS_ID = S.EOAWPRCS_ID 
    AND P.EOAWDEFN_ID = S.EOAWDEFN_ID 
    AND P.EFFDT = S.EFFDT 
    AND U.DTTM_CREATED between to_date(:d1, 'dd-mon-yy') 
      and to_date(:d2, 'dd-mon-yy') 
    AND P.EOAWSTAGE_NBR = S.EOAWSTAGE_NBR 
    AND P.EOAWPATH_ID = S.EOAWPATH_ID 
    AND P.EOAWSTEP_NBR = S.EOAWSTEP_NBR 
    GROUP BY DECODE(SUBSTR(P.DESCR,1,18),'Chartfield Signers','Chartfield Signers',P.DESCR),DECODE(U.EOAWSTEP_STATUS,'A','Approval','Denial') 
    ORDER BY DECODE(SUBSTR(P.DESCR,1,18),'Chartfield Signers','Chartfield Signers',P.DESCR),DECODE(U.EOAWSTEP_STATUS,'A','Approval','Denial'); 
+0

我看到的代碼,但沒有圖像。是否說你沒有足夠的聲望發佈圖片? –

+0

最初的3個單詞「Excel圖像參考」是圖片 –

回答

0

下面的代碼使用您的初始查詢(大部分是一個不漏地減少長度)的CTE,然後將一個軸部,這是你需要獲得批准和拒絕在自己列了什麼。

--Be sure to put a ; before WITH 
--Don't miss the) at the end of the section 

;WITH Main AS (
SELECT DECODE(SUBSTR(P.DESCR,1,18),'Chartfield Signers','Chartfield Signers',P.DESCR) AS Descr 
    , DECODE(U.EOAWSTEP_STATUS,'A','Approval','Denial') AS AppDen 
    , COUNT(U.EOAWSTEP_INSTANCE) AS CountAD, TO_CHAR(AVG(
    EXTRACT ... 
    ... 
    GROUP BY Descr, AppDen 
/* ORDER BY DECODE(SUBSTR(P.DESCR,1,18),'Chartfield Signers','Chartfield Signers',P.DESCR) 
     ,DECODE(U.EOAWSTEP_STATUS,'A','Approval','Denial')*/ 
) 
--I didn't see anywhere the string Stage was set, but I see it in your desired output. 
--I've just added it as a separate variable 

--In your code you have Approvals and Denials in separate records. You need to pivot 
--Approval and Denial to be columns on the same row. The PIVOT function will do that 
--It takes some getting used to. You should try and become familiar with it online or otherwise 

--The two COALESCE functions allow sql to use a 0 in the Approval and Denial columns when there 
--is no Approval or Denial row for a given descr. 

,PivotTable AS (
SELECT Stage 
     ,Descr 
     ,[Approval] 
     ,[Denial] 
    FROM (SELECT Stage, Descr 
     ,COALESCE([Approval],0) AS [Approval] 
     ,COALESCE([Denial],0) AS [Denial] 
     FROM (SELECT Stage, Descr, AppDen, [CountAD] 
      FROM Main) AS so 
     PIVOT(SUM(CountAD) FOR AppDen IN (
      [Approval] 
      ,[Denial] 
     )) AS p) AS p 
) 

--since the values in duration are not the same for the one example you gave us of 
--a descr with both approval and denial, you can't include that in the pivot section. 
--I've added this next section to insert the duration of the Approval row. 
--You may need to adjust it for however you want to determine Duration 

SELECT pt.Stage 
    , pt.Descr 
    , pt.Approval 
    , pt.Denial 
    , m.Duration 
    FROM PivotTable AS pt 
     LEFT JOIN Main AS m ON pt.Stage = m.Stage 
      AND pt.Descr = m.Descr 
      AND m.AppDen = 'Approval' 
    ORDER BY pt.Stage, pt.Descr 

(我用您的變量初始申報別名,以避免重複在GROUP BY代碼段。如果您的變量的變化計算,你不希望有通過代碼滾動到找到所有的不同的地方,你需要改變它。)