markvaughn2006 Posted January 27, 2011 Share Posted January 27, 2011 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!! Quote Link to comment Share on other sites More sharing options...
markvaughn2006 Posted January 27, 2011 Author Share Posted January 27, 2011 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.