Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.