Is there a way to say select * and still use sum(amount)?
Main Topics
Browse All TopicsI have the following command:
$sql ="SELECT * FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate' ORDER BY trans_date";
I want to GROUP BY `entityID`
And I want it to add up all the values in the amount field and display the total for each group. How would I accomplish this? Also, how do you get the character `?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I would use your original query and then using a subquery looks mine with the exception that you will add in blank/empty strings or null for columns before and after the amount in your query then UNION ALL the two queries and then order by your entityID and should end up with all you data then a line like this.
entityID, '', '', '', 2000.00, '', '', '', ''
Hope that helps.
{I am not a PHP master, so i apologize ahead of time}
Say you have 6 columns in your select.
$sql ="SELECT * FROM (SELECT entityID, col2, col3, col4, col5, amount FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate'";
$sql = $sql + " UNION SELECT entityID, '', '', '', '', SUM(amount) FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate' GROUP BY entityID) ORDER BY trans_date, entityID";
I did this and got a SQL syntax error.
$sql ="SELECT * FROM (SELECT entityID, trans_date, type, catID, cat_subID, payeeID, amount FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate'";
$sql = $sql + " UNION SELECT entityID, '', '', '', '','','', SUM(amount) FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate' GROUP BY entityID) ORDER BY entityID";
I took out the second line did this:
$sql ="SELECT * FROM (SELECT entityID, trans_date, type, catID, cat_subID, payeeID, amount FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate')";
Now I get an error message saying Every derived table must have its own alias.
$sql ="SELECT * FROM (SELECT entityID, trans_date, type, catID, cat_subID, payeeID, amount FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate'";
$sql = $sql + " UNION SELECT entityID, '', '', '', '','','', SUM(amount) FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate' GROUP BY entityID) derived ORDER BY entityID";
Add in an alias for the inner query like derived.
Like I said I am not a PHP person, but don't you need to have the variables outside of the string literal, something like this:
$sql ="SELECT * FROM (SELECT entityID, trans_date, type, catID, cat_subID, payeeID, amount FROM ".$table_name1." WHERE trans_date BETWEEN '".$startDate."' AND '".$endDate."')";
I've gotten the first statement to work. Can I have multiple conditions in the where clause like:
$sql ="SELECT * FROM $table_name1 WHERE (trans_date BETWEEN '$startDate' AND '$endDate') AND (entityID = '$indEntity'), ORDER BY trans_date";
I'm getting a syntax error. I'm trying the second query now. I've increased the points for your efforts.
might be "." instead of "+".
I was trying to concatenate the strings. I just used the wrong operator for PHP: http://us3.php.net/languag
Good luck. Sorry UNION didn't work out for you. As research for your new question, I would try to run these queries directly in query browser for MySQL and parse there first before transferring to PHP. Think mixing of PHP syntax to pass in variables and the issues with the query are too much to troubleshoot all at once. Make the query work with hand coded values in query browser then replace literals with $vars in PHP. If it doesn't work, then you can have PHP experts troubleshoot versus SQL ones OR vice versa if doesn't work in MySQL.
Hope that suggestion makes sense.
Respectfully yours,
Kevin
Business Accounts
Answer for Membership
by: mwvisa1Posted on 2008-09-16 at 15:53:27ID: 22494046
$sql ="SELECT entityID, SUM(amount) FROM $table_name1 WHERE trans_date BETWEEN '$startDate' AND '$endDate' GROUP BY entityID ORDER BY trans_date";
.
It is my understanding you ca just leave that off. Otherwise, you can try just typing in code. If you copied and pasted above, the character on a en-US keyboard at least is top left below escape. You ca also try HTML entity character from ASCII table: http://www.asciitable.com/
`