How to calculate grand total in mysql without group by clause in mysql Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to calculate grand total in mysql without group by clause in mysql without wasting too much if your time.

The question is published on by Tutorial Guruji team.

i want to get grand total of a column without using group by clause as i don’t want to concat any string.

i looking for this kind desired result:

|  customer id | project name | Product Name | Quantity | price  | total    |
+--------------+--------------+--------------+----------+--------+----------+
|     9        |  xyz         |   ppn        |   2      |   2    |   4      |
+--------------+--------------+--------------+----------+--------+----------+
|    11        |    pqr       |    xxx       |    2     |   2    |    4     |
+--------------+--------------+--------------+----------+--------+----------+
|              |              |              |          |        |     8    |<=== Grand total  
+---------------------------------------------------------------------------+

here is SQL Fiddle:http://sqlfiddle.com/#!9/30fdd5/2

Answer

I think this does what you want:

SELECT customer_id, project_name, product_name, quantity,
       price, quantity * price as total 
FROM project_expenses_data
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, sum(quantity * price)
FROM project_expenses_data;

To be 100% clear that the total is the last row in the result set, add the clause ORDER BY (customer_id IS NULL) ASC.

We are here to answer your question about How to calculate grand total in mysql without group by clause in mysql - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji