我有一张可以容纳大量数据的表。包含数据的新 SQL 查询将每 5 分钟插入一次。
EVENTID EVENTDATE CPU
--------- --------------------- --------
23523 2014-10-28 20:26:51 33
2234234 2014-10-28 20:27:05 22
234 2014-10-28 20:27:19 234
22 2014-10-28 20:27:29 22
234 2014-10-28 20:30:13 234
234 2014-10-28 20:38:18 23234
234 2014-10-28 21:50:06 234
234 2014-10-28 20:41:51 12312
234 2014-09-30 23:00:34 123
234 2014-11-28 21:18:23 234
234 2014-10-30 21:24:28 234
234 2014-10-29 21:27:52 23434
我想在图表中显示 30 天的数据。但我有两个问题。
我想以 30 个值的形式将所有数据的平均值显示到表中。
所以首先我需要获取每个时间间隔的平均值。其次,我想将结果限制在 30 个值内。有办法做到这一点吗?
最佳答案
select trunc(eventdate, 'DD'), avg(cpu)
from tab where eventdate >= trunc(sysdate) - 30
group by trunc(eventdate, 'DD')
order by 1;
trunc(eventdate, 'DD')
从 eventdate 中删除时间。当您按此表达式分组时,Oracle 会计算每天的平均值。
如果您想显示大约 30 行,您可以使用一些返回后续日期的特殊查询创建一个联接:
select start_date, avg(cpu)
from (select start_date + lvl start_date, start_date + lvl + 1 - 1/24/60/60 end_date
from (select trunc(sysdate-30) start_date, level lvl from dual connect by level <= 30))
left join tab on eventdate between start_date and end_date
group by start_date
order by 1;
关于sql - 获取 Oracle 中时间段的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26677852/