How can we get the summary output of a column in MySQL result set itself?


We can get the summary output of a column in MySQL result set by using the “WITH ROLLUP” modifier. This modifier is used with GROUP BY CLAUSE. It gives the summary output to include extra rows that represent higher-level summary operations.

Example

In this example, the WITH ROLLUP modifier gave the summary output with total cost value in the extra row.

mysql> Select Item_name, SUM(Cost) AS Total_cost from Item_list GROUP BY Item_name WITH ROLLUP;
+-----------+------------+
| Item_name | Total_cost |
+-----------+------------+
| Notebook  | 45.00      |
| Pen       | 31.70      |
| Pencilbox | 125.20     |
| NULL      | 201.90     |
+-----------+------------+
4 rows in set (0.00 sec)

Updated on: 22-Jun-2020

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements