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)
以下代码使用您的初始查询(大部分内容被省略以减少长度)作为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部分重复代码。如果变量的计算发生变化,您不希望必须滚动代码才能找到所有不同的地方你需要改变的地方。)