我们正在开发MYSQL,ASP.Net应用程序。我使用TempTable来存储我的存储过程的输出。
由于我有记录集表,现在我关心的是如何获得总行数& ColumnWise,并分别在新行和新列中显示它。下图将显示我所期望的输出

发布于 2013-06-05 18:38:15
试一试
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 |这是演示
发布于 2013-06-05 18:51:04
您可以使用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 |https://stackoverflow.com/questions/16935708
复制相似问题