表1
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
结果:
400, 60
600, 30
500, 30
totals, 110
我想添加总数。这是一个现有的表,我只能选择。
这是我的查询。我不知道如何添加总数
SELECT Type, SUM(Amount)
FROM table1
GROUP BY Type
最佳答案
您正在寻找带有汇总
:
select type, sum(amount)
from t
group by type with rollup;
注意:最后一组的 type
将为 NULL
,而不是 totals
。您可以使用coalesce()
来获取您想要的任何值。
关于mysql - 查询添加总计和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796689/