与人口总数的总计

时间:2017-12-26 19:38:37

标签: mysql sql

我有一张这样的表:

-----------------
| Name  | Amount|
-----------------
| A     | 100   |
| B     | 200   |
| C     | 100   |
| D     | 200   |
| E     | 100   |
| Total | 700   |
-----------------

我希望能够为每个人总计,并将其余部分显示为总数以进行比较。所以对于例如:

-----------------
| Name  | Amount|
-----------------
| A     | 100   |
| Others| 600   |
| Total | 700   |
-----------------

当我执行GROUP BYWITH ROLLUP时,我能够获得原始表格,但无法弄清楚如何生成第二张表格。< / p>

1 个答案:

答案 0 :(得分:0)

SELECT NAME, AMOUNT FROM
(
SELECT
CASE WHEN A THEN A ELSE OTHERS END AS NAME, AMOUNT
FROM MyTable
) T
GROUP BY ROLLUP NAME, AMOUNT