两个月之前需要数据库每小时数据

时间:2017-10-12 04:26:20

标签: mysql sql database datetime group-by

我有一个包含cpuload,freememory,diskspace,cpu utilization和hostname feilds的表。我正在运行一个cron作业来获取所有主机中每10分钟的数据(例如:4个主机)。 现在我在数据库中有一年的数据。

我想将该数据转换为每小时平均数据。但我只需要两个月之前的数据。最近两个月的数据不应该打扰。 我的数据就像这样

 hostname              | cpuload | freedisk  | freemem |timestamp 
 localhost.localdomain | 0.15    | 136052    | 383660  | 2017-08-01 00:10:08 
 localhost.localdomain | 0.03    | 136492    | 383660  | 2017-08-01 00:20:08 
 localhost.localdomain | 0.01    | 133456    | 383660  | 2017-08-01 00:30:08 
 localhost.localdomain | 0.10    | 64544     | 383660  | 2017-08-01 00:40:08 
 localhost.localdomain | 0.01    | 68148     | 383660  | 2017-08-01 01:00:08 

每10分钟数据将添加至今,即2017-10-12 11:00:00

现在我想拿两个月前的数据,即从2017-08-12 11:00:00到2017-08-01 00:00:00数据,我需要转换为每小时数据

SELECT Avg(CpuLoad) AS avgcpuload
    ,Avg(FreeMemory) AS avgfreemem
    ,Avg(FreeDisk) AS avgfreedisk
    ,Avg(Cpu_util) AS avgcpuutil
FROM Dynamic_Host_Information;

我得到了avg,但我想要它只用了两个月的数据。

请帮帮我。 提前致谢 sagar reddy

1 个答案:

答案 0 :(得分:1)

可以通过group by列的date and hour only部分timestamp部分where2数据与SELECT t1.hostname ,avg(cpuload) AS cpuload ,avg(freedisk) AS freedisk ,avg(freemem) AS freemem FROM table1 t1 WHERE t1.TIMESTAMP < current_timestamp - interval '42' day GROUP BY date_format(t1.TIMESTAMP, '%Y%m%d%H'); 子句一起INTERVAL '42' DAY排除以前的Oct个月数据在下面得到你想要的结果。

Sep

hostname cpuload freedisk freemem ------------------------------------------------------------- localhost.localdomain 0.072500 117636.0000 383660.0000 localhost.localdomain 0.010000 68148.0000 383660.0000 用于排除SELECT t1.hostname ,avg(cpuload) AS cpuload ,avg(freedisk) AS freedisk ,avg(freemem) AS freemem FROM table1 t1 WHERE t1.TIMESTAMP between '2017-08-01 00:00:00' and '2017-08-12 11:00:00' GROUP BY date_format(t1.TIMESTAMP, '%Y%m%d%H'); /images/* -> server 1 (only server in pool 1) /* -> server 2 (only server in pool 2) 数据。你可以相应地调整它。

<强>结果:

function totalView($id){
    $count = 1;
    $check = get_post_meta($id, 'count',true);
    if(!$check){
        add_post_meta($id,'count',$count,true);
    }else{
        $inc = $check+1;
        $a = update_post_meta($id,'count',$inc);
    }
    return 'Total views:&nbsp;<b>'.get_post_meta($id, 'count',true).'</b> &nbsp; times';
}

更新1:

如果您需要上述时间段的数据。

const profit =
  price -
  _.sumBy(
    await Promise.all(
      map(uOrder => uOrder.invoice, await order.upstreamOrders),
    ),
    'amount',
  );

您可以查看演示 here