View Full Version : How to echo row using 'WITH ROLLUP'
05-05-2009, 09:22 AM
How do I address the field created by the 'with rollup' condition?
$sql="SELECT hours,area,volunteer FROM hoursworked WITH ROLLUP";
"<p>Number of Hours Worked: " . $row['hours'] . "</p><p>Area: " . $row['area'] . "</p><p>Name of Volunteer: " . $row['volunteer'] . "</p>"';
05-05-2009, 09:49 AM
What exactly are you trying to do?
As far as I know, "WITH ROLLUP" is a modifier of "GROUP BY", which you do not use.
Furthermore, it should produce additional rows, not additional fields.
05-05-2009, 01:14 PM
Yes, it produces an extra row that has as it's key, the sum of the row it affects, but no value. Just the key. My puzzle is how to I address that new row to make us of the sum it has produced?
05-05-2009, 01:58 PM
Well, what more is there to say that I didn't already tell you? "WITH ROLLUP" is a modifier of "GROUP BY", using the former without the latter should produce an error.
And if you want a sum, you first have to get a sum.
$sql="SELECT volunteer, SUM(hours) FROM hoursworked GROUP BY volunteer WITH ROLLUP";
will give you the sum of all worked hours for each volunteer, and additionally, as the last line of the result set, the sum of all worked hours (by all volunteers).
05-05-2009, 03:16 PM
Yes, I understand what you have said. I had thought that using the built in ability to obtain a column sum would save the the step of running a second query. The first to iterate each persons record, the second to obtain the sum.
I almost had a shortcut.
However, I will continue to use the two query method.
Thanks for your help.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.