我想按15秒的时间间隔总结以下查询。
select to_char(sample_time,'hh24:mi:ss') as SAMPLE_TIME,nvl(wait_class,'CPU'),count(*)
from gv$active_session_history
group by wait_class,sample_time;
结果;
SAMPLE_TIME WAITS COUNT
-----------------------------
14:59:00 CPU 3
14:59:02 CPU 1
14:59:08 CPU 2
14:58:11 CPU 2
14:59:18 CPU 1
14:59:24 CPU 2
14:58:29 CPU 2
我要按15秒的间隔求和。我该怎么做?
SAMPLE_TIME WAITS COUNT
-----------------------------
14:59:15 CPU 8
14:59:30 CPU 5
答案 0 :(得分:0)
使用此功能:
CREATE OR REPLACE FUNCTION MakeInterval(ts IN TIMESTAMP, roundInterval IN INTERVAL DAY TO SECOND) RETURN TIMESTAMP DETERMINISTIC IS
denom INTEGER;
BEGIN
IF roundInterval >= INTERVAL '1' HOUR THEN
denom := EXTRACT(HOUR FROM roundInterval);
IF MOD(24, denom) <> 0 THEN
RAISE VALUE_ERROR;
END IF;
RETURN TRUNC(ts) + TRUNC(EXTRACT(HOUR FROM ts) / denom) * denom * INTERVAL '1' HOUR;
ELSIF roundInterval >= INTERVAL '1' MINUTE THEN
denom := EXTRACT(MINUTE FROM roundInterval);
IF MOD(60, denom) <> 0 THEN
RAISE VALUE_ERROR;
END IF;
RETURN TRUNC(ts, 'hh') + TRUNC(EXTRACT(MINUTE FROM ts) / denom) * denom * INTERVAL '1' MINUTE;
ELSE
denom := EXTRACT(SECOND FROM roundInterval);
IF MOD(60, denom) <> 0 THEN
RAISE VALUE_ERROR;
END IF;
RETURN TRUNC(ts, 'mi') + TRUNC(EXTRACT(SECOND FROM ts) / denom) * denom * INTERVAL '1' SECOND;
END IF;
END MakeInterval;
然后您就可以使用
...
GROUP BY wait_class, MakeInterval(SAMPLE_TIME, INTERVAL '15' SECOND)
答案 1 :(得分:0)
这看起来有点hack-ish,但它应该是您想要的。 lpad只是为了获得更好的外观。
编辑:忘记按实际语句而不是 sample_time 分组。现在应该以15秒为间隔进行分组。
select
to_char(sample_time, 'hh24:mi:') || lpad(trunc(to_number(to_char(sample_time, 'ss')) / 15) * 15, 2, '0') as SAMPLE_TIME,
nvl(wait_class, 'CPU') as wait_class,
count(*)
from gv$active_session_history
group by nvl(wait_class, 'CPU'), to_char(sample_time, 'hh24:mi:') || lpad(trunc(to_number(to_char(sample_time, 'ss')) / 15) * 15, 2, '0')
答案 2 :(得分:0)
-无法添加评论,因为我的代表非常低
@Wernfried Domscheit向您提供了所有必需的信息,我接受了他的评论,然后简单地按查询的一部分将它们“拍打”到了组中(显然需要在选择语句中添加存储桶定义)
下面的代码将允许您检查Wernfried的回复:
select TO_CHAR(TRUNC(SAMPLE_TIME, 'mi') + TRUNC(EXTRACT(SECOND FROM SAMPLE_TIME) /
EXTRACT(SECOND FROM INTERVAL '15' SECOND)) * EXTRACT(SECOND FROM INTERVAL '15'
SECOND) * INTERVAL '1' SECOND,'MM/DD/YYYY HH24:MI:SS') AS TimeBucket
, nvl(wait_class,'CPU')
,count(*) as NbrRecords
from gv$active_session_history
GROUP BY TO_CHAR(TRUNC(SAMPLE_TIME, 'mi') + TRUNC(EXTRACT(SECOND FROM SAMPLE_TIME) /
EXTRACT(SECOND FROM INTERVAL '15' SECOND)) * EXTRACT(SECOND FROM INTERVAL '15'
SECOND) * INTERVAL '1' SECOND,'MM/DD/YYYY HH24:MI:SS'), nvl(wait_class,'CPU')
ORDER BY TimeBucket DESC
;
HTH,
B