SQL分组数据步长为10分钟

时间:2013-09-26 14:12:04

标签: sql oracle datetime

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

以下是我的表格:

过程

  • ID
  • 名称
  • 类型

processRun

  • ID
  • 的ProcessID
  • dateStart
  • dateEnd

这是我的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,但它无效。

感谢您的帮助!


知道了!由于我的GROUP BY,我的AVG()是假的。我想出了如何通过juste在上一次AVG()选择我的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

0 个答案:

没有答案