Consider a simple query:
SELECT id, date, hours FROM employees where id=10
And the following result set:
id | date | hours
---------------------
1 4-19-2012 4
1 4-19-2012 2.5
1 4-19-2012 1
1 4-20-2012 2
1 4-20-2012 3
I’m showing a typical HTML table output of the result set but I’d like to display a daily summation on a row underneath of that days results.
+--------------------------------------+
| David | 4-19-2012 | 4 |
| | 4-19-2012 | 2.5 |
| | 4-19-2012 | 1 |
|--------------------------------------|
| TOTAL ............ 7.5 |
|--------------------------------------|
| David | 4-20-2012 | 2 |
| | 4-20-2012 | 1 |
|--------------------------------------|
| TOTAL ............ 3 |
|--------------------------------------|
| GRAND TOTAL ..........10.5 |
+--------------------------------------+
What are some of the best ways to handle that? There are many options. Subquerying… putting PHP code in my view… surely there is a bettery way.
Upgraded to an answer
In MySQL, you can add extra summary rows in a table that is grouped with
GROUP BYby using theWITH ROLLUPmodifier:With your query, you could do: