简体   繁体   中英

Mysql Php Average of temperature reads divided by hour

I have a MYSQL DB with two columns, one is the temperature and the other one is the time in which the temperatura was read, like http://pastebin.mozilla.org/1860064 . The temperature USUALLY have a 5minutes difference between reads, but this is not always with this time gap.

What I need to figure out is a algorithm to read all the values and save the average of every hour of read, so temperature will be saved on the db with one temperature per hour, 24 reads per day, and it would save in another db's table.

For example:

在此处输入图片说明

It would save with the average temperature and with the date 2012-10-07 10:00:00,

Was I clear?

Here is one way to do this with just a query. If you need the results stored in the database you'll need your own inserts but this will get you the data you're looking for.

SELECT AVG(temperature),HOUR(read_time) as read_hour FROM temperature_reads WHERE DATE(read_time) = '2012-10-06' GROUP BY read_hour

You can swap the 2012-10-06 part out for whatever date you are looking at.

Edit: Here it is updated with your info:

SELECT AVG(temperatura),HOUR(data) as read_hour FROM leituras WHERE DATE(read_time) = '2012-10-06' GROUP BY read_hour

This should do the trick!

Another way to do this is without filtering any day and including the whole day and hour on the grouping clause:

SELECT AVG(temperature),
       DATE_FORMAT(date, '%d/%m/%Y %H') day_hour_read
  FROM TABLE
GROUP BY DATE_FORMAT(date, '%d/%m/%Y %H')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM