In MYSQL GROUP BY provide us a modifier called WITH ROLLUP, which will added an extra row to the output as a summary report. For Example:
Suppose we have a Sales table having SalesId, ContactId and SalesTotal. If we need a summary report, which will display total SalesTotal group by its ContactId.
This can be done using a simple query: SELECT IFNULL(`ContactId` , “GRAND TOTAL”) AS ‘CONTACT ID’, SUM(`SalesTotal`) AS `TOTAL` FROM Sales GROUP BY `ContactId` WITH ROLLUP
|
||||||||||
Output:
You can’t use the ORDER BY clause with the WITH ROLLUP. They are mutually exclusive.
|