2017-04-19 133 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 
相關問題