NaD NaD - 11 days ago 5
MySQL Question

Query for adding totals sum

Table1

ID, ANumber, Type, Amount, Date
1, 00010, 400, 10, 2016-11-16
2, 00011, 600, 20, 2016-11-12
3, 00012, 600, 10, 2016-11-13
4, 00013, 500, 30, 2016-11-17
5, 00014, 400, 40, 2016-11-19


Results:

400, 60
600, 30
500, 30
totals, 110


I want to add the totals. this is an existing table i can only SELECT.

This is my query. i don't know how to add the totals

SELECT Type, SUM(Amount)
FROM table1
GROUP BY Type

Answer

You are looking for with rollup:

select type, sum(amount)
from t
group by type with rollup;

Note: The final group will have NULL for the type rather than totals. You can use coalesce() to get whatever value you want.