在oracle中平均一组人员

时间:2015-08-10 15:52:05

标签: oracle group-by aggregate average

我正在处理一个查询,以显示一周内我们服务器的一般用法。更具体地说,我试图在一周中的某一天找到给定小时内的平均请求数。我知道平均功能在那里,但试图仅平均每个组的成员是一个麻烦。当我运行此查询时:

SELECT DOW, HH24, AVG(LISTAGG(SV_REQUESTS) WITHIN GROUP (ORDER BY SV_REQUESTS))
  FROM
    (SELECT
      COUNT(*) AS SV_REQUESTS,  
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'MM') AS MM,
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'DD') AS DD,
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'HH24') AS HH24,
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'D-DAY') AS DOW
    FROM ACTIVITY_ACCUMULATOR
    WHERE TIMESTAMP > to_date('01-08-2015','DD-MM-YYYY')
    GROUP BY
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'HH24'),
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'MM'),
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'DD'),
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'D-DAY'))
GROUP BY DOW, HH24;

我明白了:

ORA-00937: not a single-group group function
00937. 00000 -  "not a single-group group function"
*Cause:    
*Action:

有人可以帮忙吗?我是否需要另一个子查询来正确执行此操作?

1 个答案:

答案 0 :(得分:1)

我相信你只想要avg(sv_requests)。聚合函数(例如avgcount)固有地对您要分组的行组进行操作。因此,对于DOWHH24的每个不同组合,相应行的sv_requests将被平均。

SELECT DOW, HH24, AVG(SV_REQUESTS)
  FROM
    (SELECT
      COUNT(*) AS SV_REQUESTS,  
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'MM') AS MM,
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'DD') AS DD,
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'HH24') AS HH24,
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'D-DAY') AS DOW
    FROM ACTIVITY_ACCUMULATOR
    WHERE TIMESTAMP > to_date('01-08-2015','DD-MM-YYYY')
    GROUP BY
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'HH24'),
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'MM'),
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'DD'),
      TO_CHAR(TO_DATE(TIMESTAMP,'DD-MM-YYYY HH24:MI:SS'),'D-DAY'))
GROUP BY DOW, HH24;