跨表Peoplesoft / Oracle SQL乘以

时间:2017-02-28 18:53:52

标签: sql peoplesoft

我正在尝试为Peoplesoft编写一个SQL,它具有用户在一段时间内在查询中使用表(RECNAME)的次数。

从PSQRYRECORD我可以检索QRYNAME& RECNAME(QRYNAME可以有多个RECNAME)

从PSQRYSTATS我可以检索到QRYNAME& EXECCOUNT(QRYNAME只有1个EXECCOUNT),由OPRID&过滤LASTEXECDTTM。

但这给了我太高的金额:

select A.RECNAME, Sum(B.EXECCOUNT) from FMSADM.PSQRYRECORD A, FMSADM.PSQRYSTATS B
WHERE A.QRYNAME = B.QRYNAME
AND  A.OPRID = 'JDOE'
AND B.LASTEXECDTTM BETWEEN to_date('01-JAN-16 00:00:00','DD-Mon-YY HH24:MI:SS') AND to_date('27-FEB-17 23:50:00','DD-Mon-YY HH24:MI:SS')
GROUP BY A.RECNAME
ORDER BY Sum(B.EXECCOUNT) DESC

我想我需要计算RECNAME,然后乘以EXECCOUNT,但无法让它工作。

谢谢!

1 个答案:

答案 0 :(得分:1)

问题是PSQRYRECORD可能在多行中具有相同的OPRID,QRYNAME,RECNAME组合。当它执行时,您的execcount将乘以该查询。您需要从PSQRYRECORD获取不同的OPRID,QRYNAME,RECNAME,然后将其加入PSQRYSTATS

WITH QRYREC AS (
    SELECT OPRID, QRYNAME, RECNAME
    FROM PSQRYRECORD
    GROUP BY OPRID, QRYNAME, RECNAME
)
SELECT QRYREC.RECNAME, SUM(QS.EXECCOUNT)
FROM QRYREC
JOIN PSQRYSTATS QS ON QS.QRYNAME = QRYREC.QRYNAME
WHERE
QRYREC.OPRID = 'JDOE'
AND QS.LASTEXECDTTM BETWEEN to_date('01-JAN-16 00:00:00','DD-Mon-YY HH24:MI:SS') AND to_date('27-FEB-17 23:50:00','DD-Mon-YY HH24:MI:SS')
GROUP BY QRYREC.RECNAME
ORDER BY SUM(QS.EXECCOUNT) DESC