php - Trying to take the average of 60 points in an hour, then output in graph -


i have database running, gets new data stored every minute. so, in 1 hour there 60 entries. entries, i'm trying generate average per hour (using timestamp), haven't got faintest on how go doing that.

here's mysql selection code getting data last hour:

$value = "select `brutovalue`, `nettovalue`, `regdate`                     `mydb`                      regdate > date_sub(                      current_timestamp , interval 1 hour )"; $result = $dbhandle->query($value); 

and here's mysql code getting data last week (168 hours in week, right?):

$valueweek = "select `brutovalue`, `nettovalue`, `regdate`                     `mydb`                      regdate > date_sub(                      current_timestamp , interval 168 hour )"; $resultweek = $dbhandle->query($valueweek); 

the latter renders extensive list 10080 entries (168 * 60). need every hour, 60 entries summed up, divided 60, , result added array of average values on past 168 hours.

while got working last hour, can't figure out how take averages. code storing values of last hour arrays can use graph:

if ($result->num_rows > 0) {         // output data of each row         while($row = $result->fetch_assoc()) {             $datearray[] = $row["regdate"];             $brutoval[] = $row["brutovalue"];             $nettoval[] = $row["nettovalue"];         }       } 

could me out please? i'm @ loss here. in advance.

you use mysql's date functions group date , hour. notice group clause @ end , avg() usage in columns.

$valueweek = "select avg(`brutovalue`) `brutovalue`, avg(`nettovalue`) `nettovalue`, concat(date(`regdate`), '_', hour(`regdate`)) `date_hour`                     `mydb`                      regdate > date_sub(                      current_timestamp , interval 168 hour )                     group date(`regdate`), hour(`regdate`)"; 

your return data should have single row every hour of day(that in db) averaged value bruto , netto.

edit: changed mysql above have concatenated value of date_hour 2016-02-08_17, output data array during while loop (to match previous setup) like:

while($row = $result->fetch_assoc()) {     $datearray[] = $row["date_hour"];     $brutoval[] = $row["brutovalue"];     $nettoval[] = $row["nettovalue"]; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -