need database hourly data before two months

那年仲夏 提交于 2019-12-13 03:33:01

问题


I have a table with cpuload,freememory,diskspace, cpu utilization and hostname feilds. i am running a cron job to get the data for every 10 mins in all hosts(ex: 4 hosts). Now i have one year data in database.

i want to convert that data into hourly average data. but i need only for the data which is before two months. last two months data should not disturb. my data is like this

 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 

every 10 mins data will add till today i.e 2017-10-12 11:00:00

now i want to take two months ago data i.e from 2017-08-12 11:00:00 to 2017-08-01 00:00:00 data i need to convert to hourly data

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

i got avg but i want it for two months back data only.

please help me in this. thanks in advance sagar reddy


回答1:


One way could be to group by your data by date and hour only part of your timestamp column along with a where clause to exclude previous 2 months data as below to get your desired result.

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 is used to exclude Oct and Sep data. You can adjust it accordingly.

Result:

hostname                cpuload     freedisk      freemem
-------------------------------------------------------------
localhost.localdomain   0.072500    117636.0000   383660.0000
localhost.localdomain   0.010000    68148.0000    383660.0000

Update 1:

If you need data for the time period mentioned in question.

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');

You can check the demo here



来源:https://stackoverflow.com/questions/46701403/need-database-hourly-data-before-two-months

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!