1

我想对过去一小时内启动的进程的平均持续时间、名称和 ID 进行排序,按 15 分钟的步长进行排序和分组

这是我的桌子:

过程

  • ID
  • 姓名
  • 类型

进程运行

  • ID
  • 进程号
  • 日期开始
  • 日期结束

这是我的 sql 查询:

WITH tps AS (
       SELECT sysdate-1/24,trunc(TO_DATE((trunc(sysdate,'MI')-MOD(TO_CHAR(sysdate, 'MI'),15)/(24*60))-1/24) + (level) / 96, 'mi') AS DATEREF
       FROM dual
       CONNECT BY level <= 96 * (TO_DATE(SYSDATE) - TO_DATE(((trunc(sysdate,'MI')-MOD(TO_CHAR(sysdate, 'MI'),15)/(24*60))-1/24)-1/24))
    ),
da AS (
        SELECT p.id as Id, p.Name as Nom,trunc(pRun.DateStart,'MI') as dateStart, AVG(pRun.DATEEND - pRun.DATESTART)*1440 as VAL
        FROM process p
        JOIN processrun pRun ON pRun.processid = p.id
        WHERE trunc(pRun.DateStart,'MI') BETWEEN trunc(SYSDATE-1/24,'MI') AND trunc(SYSDATE,'MI')
        AND p.Id = 92
   )
SELECT da.Id, da.Nom, da.VAL, da.dateStart, tps.dateref
FROM tps
LEFT JOIN da ON da.dateStart BETWEEN tps.dateref AND tps.dateref-15/1440
GROUP BY tps.dateref
ORDER BY da.dateStart, da.Nom ASC

这导致:

SELECT da.Id, da.Nom, da.VAL, da.dateStart, tps.dateref
ERROR at line 13:
ORA-00979: not a GROUP BY expression

*指示器位于 下方da.Id

我知道我没有正确执行此操作,但我想按 10 分钟的步骤对结果进行分组,我真的不知道如何使用该WITH子句来修复它。通常,我只会使用GROUP BY tps.dateref,但它不起作用。

谢谢您的帮助!


知道了!myGROUP BY是错误的,因为我的AVG(). 我想出了如何通过 juste select myAVG()在最后一个解决它SELECT这是我现在的查询:

WITH tps AS (
   SELECT trunc(TO_DATE((trunc(sysdate,'MI')-MOD(TO_CHAR(sysdate, 'MI'),15)/(24*60))-1/24) + (level) / 96, 'mi') AS DATEREF
   FROM dual
   CONNECT BY level <= 96 * (TO_DATE(SYSDATE) - TO_DATE(((trunc(sysdate,'MI')-MOD(TO_CHAR(sysdate, 'MI'),15)/(24*60))-1/24)-1/24))
),
da AS (
    SELECT p.id as Id, p.Name as Nom,trunc(pRun.DateStart,'MI') as dateStart, pRun.DATEEND - pRun.DATESTART as VAL
    FROM process p
    JOIN processrun pRun ON pRun.processid = p.id
    WHERE trunc(pRun.DateStart,'MI') BETWEEN  trunc(SYSDATE-1/24,'MI') AND  trunc(SYSDATE,'MI')
    AND p.Id = 92
 )
SELECT da.Id,da.Nom, AVG(da.VAL)*1440,tps.dateref
FROM tps
LEFT JOIN da ON da.dateStart BETWEEN tps.dateref-15/1440 AND tps.dateref
GROUP BY da.Id, da.Nom, tps.dateref
ORDER BY tps.dateref, da.Nom ASC
4

0 回答 0