Jump to content

Finding AVG of past 10 cells


markvaughn2006

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/225843-finding-avg-of-past-10-cells/
Share on other sites

as usual... the simple act of posting my question, leads me to the answer, solved...

 

for anyone else that needs the answer to this question, the code I posted works, but for what I was doing, i just replaced the static number with a variable... silly

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.