Forum Moderators: coopster

Message Too Old, No Replies

Subtotals using sum() and GROUP BY, need grand total

Am I missing something obvious?

         

shs_cmcl

5:11 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



I have a query pulling from three tables. I've used sum() on one of the table fields ('value') and GROUP BY another field ('zone') at the end of the query, so I can output value subtotals for each zone, but I'm trying to get a "grand total" I can echo at the end of the report. Am I missing something obvious? I have tried using the PHP function array_sum, after the while loop, but that hasn't worked. Here's my code:

$query = 'select *, sum(value) from table1, table2, table3 where table1.blah = table2.blah and table2.blee = table3.blee group by zone';
$result = mysql_query($query);

if ($result && mysql_num_rows($result)) {

$num_results = mysql_num_rows($result);

echo '<P>There are '.$num_results.' zones: <P>';

while ($row = mysql_fetch_array($result)) {
echo 'Zone ID: '.$row['zone'];
echo '<BR>Total Amount for This Zone: '.$row['value'];
echo '<HR>';
}
// This is where I want to echo the grand total.
}
else {
echo 'Sorry, there are no results.';
}


This is giving me pretty much exactly what I want, but how can I get a grand total of the amount field? Do I need to create a separate query, without the GROUP BY clause? I was hoping not to have to do that, I'd like to keep this as simple as possible. If I try to echo "$row['value']" after the while loop, it doesn't echo anything; I've tried various ways of array_sum($result, $num_rows, 'value') and so forth but it either says the parameter is incorrect or that "the argument must be an array." Any help?

syber

5:42 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



If you don't want to run a separate query, you could just add it as an additional column.


select *, sum(value), (select sum(value) from table1, table2, table3
where table1.blah = table2.blah
and table2.blee = table3.blee) as grandtotal
from table1, table2, table3
where table1.blah = table2.blah
and table2.blee = table3.blee
group by zone

cameraman

5:43 pm on Feb 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where you're doing this:
$row = mysql_fetch_array($result)

you're retrieving an associative array of one row from the database table. Each time you loop, it overwrites the previous data. When mysql has no more row data to give you, it returns false, which is why you can't echo $row['value'] after the while loop.

You could also sum your values as you're executing the loop. Set a variable to zero before the loop, and inside the loop add $row['value'] to it.

shs_cmcl

6:24 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



you can't echo $row['value'] after the while loop.

That occurred to me after posting (of course). Duh!

Your suggestion did the trick, I just set a variable $blah = 0 outside the loop, and inside just did $blah = ($blah + $row['value']); echoing $blah at the end, outside the loop, returns the grand total just like I wanted. Thanks!