Using AVG function
Moderator: General Moderators
-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
Using AVG function
Hey guys using the avg function in some code And need to adjust it. Making a php script for baseball averages. I need to change the Average function, so the math in it is hits divided by ab or [h]/[ab]. Right now I just have the avgAVG function which works but won;t be accurate cause it takes all players into account even if they have no average....here is the code part
-----------------------------
$result = mysql_query ("SELECT round(AVG(avg), 3) as AVG[avg], SUM(ab) as abSum, SUM(h) as hSum, SUM(hr) as hrSum, SUM(rbi) as rbiSum, SUM(r) as rSum, SUM(doubles) as doublesSum, SUM(triples) as triplesSum, SUM(sb) as sbSum from hit WHERE team='Ana'");
--------------------------------
I need the round still in there in the beginning part of the query, but i need to get the avg(avg) out and replace it with the actual formula, h divided by at bats. Can anyone give me a tip on how to do this math function???
THANKS AGAIN!!!!
you guys even helped with the rounding and sums in the beggining thanks again!!!!!!!!!!!!!!!
-----------------------------
$result = mysql_query ("SELECT round(AVG(avg), 3) as AVG[avg], SUM(ab) as abSum, SUM(h) as hSum, SUM(hr) as hrSum, SUM(rbi) as rbiSum, SUM(r) as rSum, SUM(doubles) as doublesSum, SUM(triples) as triplesSum, SUM(sb) as sbSum from hit WHERE team='Ana'");
--------------------------------
I need the round still in there in the beginning part of the query, but i need to get the avg(avg) out and replace it with the actual formula, h divided by at bats. Can anyone give me a tip on how to do this math function???
THANKS AGAIN!!!!
you guys even helped with the rounding and sums in the beggining thanks again!!!!!!!!!!!!!!!
-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
why dont you just get the sum of h and Ab, and then do it yourself:
Code: Select all
$h = $row['hSum'];
$ab = $row['abSum'];
echo$h/$ab;-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
Ya that looks like the easier way to go,,,but how do I incorporate that, into the ehco I already have???
echo "<tr bgcolor=\"$color\"><td><b>$row[avgAvg]</b></td><td><b>$row[hSum]</b></td><td><b>$row[abSum]</b></td><td><b>$row[hrSum]</b></td><td><b>$row[rbiSum]</b></td><td><b>$row[rSum]</b></td><td><b>$row[doublesSum]</b></td><td><b>$row[triplesSum]</b></td><td><b>$row[sbSum]</b></td></tr>";
Thanks in Advance!!!
echo "<tr bgcolor=\"$color\"><td><b>$row[avgAvg]</b></td><td><b>$row[hSum]</b></td><td><b>$row[abSum]</b></td><td><b>$row[hrSum]</b></td><td><b>$row[rbiSum]</b></td><td><b>$row[rSum]</b></td><td><b>$row[doublesSum]</b></td><td><b>$row[triplesSum]</b></td><td><b>$row[sbSum]</b></td></tr>";
Thanks in Advance!!!
-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
When you use the fetch_array function, aloing with signing the values of each row to variables, ie: $row, you can only use one column name.warriors2003 wrote:<td><b>$row[hSum/abSum]</b></td>
I tried that but it doesn;t work
do you have a column named hSum/abSum? I doubt it. Ill spelled it out for you. do your part and try to learn n examine the code.
Code: Select all
<?php
$h = $row['hSum'];
$ab = $row['abSum'];
echo$h/$ab;
?>Your way off base and confused.warriors2003 wrote:Not quite following you tim,,i want trying to calculate the average more precisely by taking the total hits then dividing by total AB's. I figure they have to go in the same echo???
an echo only outputs something, you can have as many as u PLEASE.
echo "hi";
echo "tim";
echo "your the best ever, joking";
hope that clears it up for you
-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
Code: Select all
<?php
$h = $row['hSum']; // ills code
$ab = $row['abSum'];
//this can be anywhere on the page after the mysql_fetch_array cause the fetch_array helps point the row grabbing
// the code you have:
echo "<tr bgcolor="$color"><td><b>$row[avgAvg]</b></td><td><b>$row[hSum]</b></td><td><b>$row[abSum]</b></td><td><b>$row[hrSum]</b></td><td><b>$row[rbiSum]</b></td><td><b>$row[rSum]</b></td><td><b>$row[doublesSum]</b></td><td><b>$row[triplesSum]</b></td><td><b>$row[sbSum]</b></td>"; // ends your echo, but not your table
echo "<td><b>$h/$ab; </b></td></tr>"; // the new echo that incorperates in your table with ills variable.
?>Code: Select all
<?php
echo "<tr bgcolor="$color"><td><b>$row[avgAvg]</b></td><td><b>$row[hSum]</b></td><td><b>$row[abSum]</b></td><td><b>$row[hrSum]</b></td><td><b>$row[rbiSum]</b></td><td><b>$row[rSum]</b></td><td><b>$row[doublesSum]</b></td><td><b>$row[triplesSum]</b></td><td><b>$row[sbSum]</b></td><td><b>$h/$ab </b></td></tr>";
?>-
warriors2003
- Forum Commoner
- Posts: 38
- Joined: Wed Mar 24, 2004 7:24 pm
I really really appreciate you trying to help. hehehe,,thanks for making it easy to just copy and paste too!!!..
.....Unfortunately I tried both,,,,and it won;t work, it just displays the / that division sign thats it, so something is funky in the echo. it's my problem tho, not yours, so don;t sweat it!!!..Thanks alot bro!!!!
.....Unfortunately I tried both,,,,and it won;t work, it just displays the / that division sign thats it, so something is funky in the echo. it's my problem tho, not yours, so don;t sweat it!!!..Thanks alot bro!!!!