我有一个带有时间戳(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
尝试这样的事情:
SELECT TO_CHAR("Date", 'HH24'), AVG("Duration") FROM "Table" GROUP BY TO_CHAR("Date", 'HH24')
您正在寻找 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;
注意: extract(hour from systimestamp) 将减去时区偏移量