按小时汇总毫秒数

时间:2013-11-08 10:18:33

标签: oracle oracle11g oracle10g

我得到了一个带有时间戳(dd.mm.yyyy hh24:mi:ss)和持续时间(以毫秒为单位)的tbl

我怎样才能将这些持续时间聚合在一起呢?它应该告诉我每小时的AVG毫秒数。

示例:

Timestamp              Duration
10.10.2010 12:30:20    200
10.10.2010 12:59:10    400

输出:

Timestamp              Duration
10.10.2010 12:00:00    300

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

SELECT TO_CHAR("Date", 'HH24'), AVG("Duration") FROM "Table" GROUP BY TO_CHAR("Date", 'HH24')

答案 1 :(得分:1)

您正在寻找ROUND和AVG

WITH DATASET
    AS (SELECT TO_TIMESTAMP_TZ ( '11/8/2013 1:16:22', 'MM/DD/YYYY HH24:MI:SS' ) AS TS,
               200 AS DURATION
        FROM DUAL
        UNION ALL
        SELECT TO_TIMESTAMP_TZ ( '11/8/2013 1:16:22', 'MM/DD/YYYY HH24:MI:SS' ) AS TS,
               100 AS DURATION
        FROM DUAL
        UNION ALL
        SELECT TO_TIMESTAMP_TZ ( '11/8/2013 2:16:22', 'MM/DD/YYYY HH24:MI:SS' ) AS TS,
               300 AS DURATION
        FROM DUAL
        UNION ALL
        SELECT TO_TIMESTAMP_TZ ( '11/8/2013 3:16:22', 'MM/DD/YYYY HH24:MI:SS' ) AS TS,
               500 AS DURATION
        FROM DUAL
        UNION ALL
        SELECT TO_TIMESTAMP_TZ ( '11/8/2013 4:16:22', 'MM/DD/YYYY HH24:MI:SS' ) AS TS,
               600 AS DURATION
        FROM DUAL
        UNION ALL
        SELECT TO_TIMESTAMP_TZ ( '11/8/2013 4:16:22', 'MM/DD/YYYY HH24:MI:SS' ) AS TS,
               700 AS DURATION
        FROM DUAL)
SELECT ROUND ( TS, 'Hh24' ) AS TIMESTAMP_VAL,
       AVG ( DURATION ) AS DURATION
FROM
      DATASET
GROUP BY
      ROUND ( TS, 'Hh24' )
ORDER BY
      TIMESTAMP_VAL;

注意:提取(来自systimestamp的小时)将减去时区偏移量