Hi I have a DB with following data:
Table: "history_data"
VALUE , DATE
100 , 2010-10-01 00:00:00
105 , 2010-10-01 00:00:05
106 , 2010-10-01 00:00:08
103 , 2010-10-01 00:00:10
If I do a simple AVG with MYSQL ( SELECT AVG('VALUE') AS CURRENT_AVG FROM "history_data") it results "103.5" that it's wrong because I need a weighted average based on datetime (from 2010-10-01 00:00:05 to 2010-10-01 00:00:07 the value still equal to 105)
The right operation to do this in math is:
$values = [100,100,100,100,105,105,105,106,106,103];
echo array_sum($values) / count($values);
results: 103 that is right....
I have created two array one with Value and Date index, another only with dateinterval in php. Than I merge it and after process it... but it's to resources expensive.... ( in production I need to make an weighted average of 12 hours. 43 200 seconds...)
Where is the most efficiently method to do this?