2013-06-05 27 views
0

我们正在开发MYSQL,ASP.Net应用程序。 我正在使用TempTable来存储我的存储过程的输出。MYSQL中RowWise&ColumnWise输出表的总数

由于我有记录集表,现在我关心的是如何获得TOTAL rowwise & ColumnWise &分别显示在新行&新列。 下面的图片将显示,我希望

enter image description here

回答

0

尝试的输出

SELECT c1, c2, c3, c4, c5, c1+c2+c3+c4+c5 c6 
    FROM table1 
UNION ALL 
SELECT SUM(c1), SUM(c2), SUM(c3), SUM(c4), SUM(c5), SUM(c1+c2+c3+c4+c5) 
    FROM table1 

输出:

| C1 | C2 | C3 | C4 | C5 | C6 | 
--------------------------------------- 
| 45 | 416 | 0 | 0 | 0 | 461 | 
| 7887 | 0 | 0 | 0 | 0 | 7887 | 
| 444 | 0 | 1628 | 0 | 0 | 2072 | 
| 8376 | 416 | 1628 | 0 | 0 | 10420 | 

这里是SQLFiddle演示

+0

Petern:谢谢你,你帮了我很多..我很欣赏..谢谢 –

+0

@ConstantLearner不客气。它有帮助吗? – peterm

+0

@Petern:是的! –

0

您可以使用GROUP BY WITH ROLLUP与总计获得的行,然后添加列连同总计获得柱:

select 
    coalesce(col1, 'Total') col1, 
    sum(col2) col2, 
    sum(col3) col3, 
    sum(col4) col4, 
    sum(col5) col5, 
    sum(col1 + col2 + col3 + col4 + col5) Total 
from yt 
group by col1 with rollup; 

SQL Fiddle with Demo。结果:

| COL1 | COL2 | COL3 | COL4 | COL5 | TOTAL | 
---------------------------------------------- 
| INCust | 45 | 416 | 0 | 0 | 461 | 
| none | 444 | 0 | 1628 | 0 | 2072 | 
| venddd | 7887 | 0 | 0 | 0 | 7887 | 
| Total | 8376 | 416 | 1628 | 0 | 10420 | 
+0

我会用这个,让你知道..无论如何,谢谢! –