使用百分比将每小时数据汇总到一天

时间:2018-03-27 16:36:37

标签: sql oracle

根据以下数据,我们如何转换为CPU闲置和使用的百分比,汇总到一天?

例如,我正在寻找聚合状态以显示当天18-JUN-15的CPU利用率为97%空闲且10%忙碌,第19天-JUN-15%为80%空闲且20%忙碌......等等

18-JUN-15 05.00.15.571 AM                                                        98.75        .69        .55         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 06.00.18.183 AM                                                        98.37          1        .63         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 07.00.03.804 AM                                                        97.67       1.56        .76         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 08.00.07.987 AM                                                        97.78       1.46        .75         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 09.00.00.707 AM                                                         97.8       1.44        .75         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 10.00.03.628 AM                                                        97.06       2.09        .84         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 11.00.06.660 AM                                                        97.92       1.36        .71         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 12.00.09.538 PM                                                        97.93       1.34        .72           0                                                                                                                                                                                                                                                                                        
18-JUN-15 01.00.12.455 PM                                                        97.95       1.31        .73         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 02.00.15.177 PM                                                        97.92       1.34        .73         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 03.00.17.829 PM                                                        98.11       1.18         .7         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 04.00.20.657 PM                                                        98.58        .82        .59         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 05.00.23.284 PM                                                        94.32       4.29       1.38         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 06.00.26.366 PM                                                        91.97       6.38       1.64         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 07.00.30.108 PM                                                        98.51        .88         .6         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 08.00.34.282 PM                                                        98.78        .67        .55         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 09.00.02.411 PM                                                        96.39       3.01        .59         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 10.00.05.019 PM                                                        97.91       1.53        .55         .01                                                                                                                                                                                                                                                                                        
18-JUN-15 11.00.07.827 PM                                                        98.64        .79        .56         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 12.00.10.329 AM                                                        98.07       1.27        .65           0                                                                                                                                                                                                                                                                                        
19-JUN-15 01.00.12.954 AM                                                        98.65        .79        .56           0                                                                                                                                                                                                                                                                                        
19-JUN-15 02.00.15.499 AM                                                         96.9       2.23        .86         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 03.00.18.041 AM                                                        98.75        .68        .57         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 04.00.01.618 AM                                                        98.72        .71        .56         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 05.00.05.221 AM                                                        98.73         .7        .56         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 06.00.08.719 AM                                                        98.48        .91        .61         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 07.00.12.035 AM                                                        97.39       1.79        .81         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 08.00.14.827 AM                                                        97.42       1.76        .81         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 09.00.17.804 AM                                                         97.4       1.76        .83         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 10.00.20.560 AM                                                        97.98       1.29        .72         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 11.00.02.756 AM                                                        98.07       1.23         .7         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 12.00.07.015 PM                                                         98.1        1.2         .7         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 01.00.10.953 PM                                                        98.03       1.24        .72         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 02.00.15.406 PM                                                        97.84       1.42        .73         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 03.00.19.628 PM                                                        98.03       1.25        .72         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 04.00.24.111 PM                                                        98.56        .83         .6         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 05.00.28.252 PM                                                        94.54       4.13       1.33           0                                                                                                                                                                                                                                                                                        
19-JUN-15 06.00.32.564 PM                                                        89.79        8.2          2         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 07.00.01.336 PM                                                         98.6         .8        .59         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 08.00.05.105 PM                                                        98.76        .68        .55         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 09.00.07.931 PM                                                        96.08       3.29        .62         .01                                                                                                                                                                                                                                                                                        
19-JUN-15 10.00.10.508 PM                                                        97.87       1.57        .56         .01  

2 个答案:

答案 0 :(得分:1)

基本汇总与链接问题相同:

select trunc(begin_interval_time) as day,
  avg(idle_time) as avg_idle,
  100 - avg(idle_time) as avg_busy
from your_table
group by trunc(begin_interval_time)
order by trunc(begin_interval_time);

DAY          AVG_IDLE   AVG_BUSY
---------- ---------- ----------
2015-06-18 97.4926316 2.50736842
2015-06-19 97.5113043 2.48869565

这使得'忙'与闲置相反;你可以改为添加其他三个值:

avg(user_time + sys_time + iowait_time) as avg_busy

但两者通常不会合计为100,因为值的精度不够高。

假设“上午9点至下午5点”意味着从09:00:00:00到16:59:59.999,您可以过滤9到16之间的小时:

select trunc(begin_interval_time) as day,
  avg(idle_time) as avg_idle,
  100 - avg(idle_time) as avg_busy
from your_table
where extract(hour from begin_interval_time) between 9 and 16
group by trunc(begin_interval_time)
order by trunc(begin_interval_time);

DAY          AVG_IDLE   AVG_BUSY
---------- ---------- ----------
2015-06-18   97.90875    2.09125
2015-06-19   98.00125    1.99875

如果您想查看工作时间和非工作时间,您可以起诉案件表达式:

select trunc(begin_interval_time) as day,
  avg(case when extract(hour from begin_interval_time)
    between 9 and 16 then idle_time end) as avg_idle_work,
  100 - avg(case when extract(hour from begin_interval_time)
    between 9 and 16 then idle_time end) as avg_busy_work,
  avg(case when extract(hour from begin_interval_time)
    not between 9 and 16 then idle_time end) as avg_idle_non_work,
  100 - avg(case when extract(hour from begin_interval_time)
    not between 9 and 16 then idle_time end) as avg_busy_non_work
from your_table
group by trunc(begin_interval_time)
order by trunc(begin_interval_time);

DAY        AVG_IDLE_WORK AVG_BUSY_WORK AVG_IDLE_NON_WORK AVG_BUSY_NON_WORK
---------- ------------- ------------- ----------------- -----------------
2015-06-18      97.90875       2.09125             97.19              2.81
2015-06-19      98.00125       1.99875             97.25              2.75

如果这不是您对工作时间的意思,您可以修改extract()来电,将其拆分到您需要的任何地方。

可能想要在计算过程中略微调整时间,具体取决于时间戳代表的内容;例如截断到小时级别,然后可以减去一个小时。这取决于是否说'19 -JUN-15 12.00.10.329 AM'应该计入18或19。如果你只是看工作时间,那可能无关紧要,你可能也不在乎......

答案 1 :(得分:0)

这将仅在每天上午9点到下午5点之间汇总每天的空闲时间。在24小时时间格式中,16与小于5pm相同。

Select TRUNC(BEGIN_INTERVAL_TIME) as BEGIN_DATE,
AVG(IDLE_TIME) as AVG_IDLE_TIME
From tbl 
Where TO_CHAR(BEGIN_INTERVAL_TIME, 'HH24') Between 9 and 16
Group by TRUNC(BEGIN_INTERVAL_TIME)