I am trying to calculate a moving average. Example:
Column names: id, date, price
$result = mysql_query("SELECT * FROM mytable WHERE id > '10' ") or die(mysql_error());
while($row = mysql_fetch_array($result)){
$thisid = $row['id'];
$sql2 = "SELECT AVG(price) FROM mytable WHERE id < '$thisid' ORDER BY id DESC LIMIT '10' ";
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
echo $row2['AVG(price)']."<br>";
}
So, what this code is intended to do, is go through each row from mytable, and calculate an average of the 10 cells with an id less than $thisid. But.... no worky..
It is coming up with some kind of an average but it is not correct. The number it comes up with is close though, but wrong enough that it is definitely doing something wrong. Any help would be extremely appreciated!!