2017-04-19 89 views
0

我尝试从我的表中选择并汇总以获得Total,但是我失败了。这里是我的表和值 enter image description heremysql select * from table group id by rollup

,结果我想这样

enter image description here

这可能吗? 我的查询:

select * from mytable group by id with rollup; 

但我查询未能获得累积值,请告诉我怎么感谢

+0

您必须在列 –

+0

上使用聚合函数总和请参见https://meta.stackoverflow.com/quest离子/ 333952/why-should-i-provide-an-mcve-for-what-seem-to-the-very-simple-sql-query – Strawberry

回答

3

试试这个:

select id,sum(qty),sum(import),sum(loss),sum(results) 
from mytable group by id asc with rollup; 
+0

Thanks @reds is really helpful –

1

你可以做类似下面

select coalesce(CAST(id as CHAR(50)),'Total'), 
max(local),sum(import),sum(loss),sum(results) from mytable group by id asc with rollup