汇总多个列和行

时间:2018-01-07 18:56:59

标签: mysql group-by sum

我在查询数据库方面遇到大麻烦。

我有一张这样的表:

+----------------------------------------+---------+---------+---------+
| name                                   | spent_1 | spent_2 | spent_3 |
+----------------------------------------+---------+---------+---------+
| Category A                             |    NULL |  124000 |   10884 |
| Category A                             |    NULL |   25630 |    9779 |
| Category A                             |    NULL |    NULL |    NULL |
| Category A                             |       0 |       0 |       0 |
| Category A                             |   75000 |    NULL |    NULL |
| Category A                             |    4000 |    NULL |    NULL |
| Category A                             |    2700 |    NULL |    NULL |
| Category B                             |   10585 |   76868 |   15037 |
| Category B                             |    NULL |    NULL |    NULL |
| Category B                             |       0 |       0 |    NULL |
| Category B                             |    5500 |    NULL |    NULL |
| Category C                             |  100000 |    NULL |  140000 |
| Category C                             |  160000 |    NULL |    NULL |
| Category C                             |    NULL |    NULL |    NULL |
| Category C                             |    NULL |    NULL |    NULL |
| Category C                             |       0 |       0 |    NULL |
| Category C                             |    NULL |    NULL |    NULL |
| Category C                             |  100000 |    NULL |    NULL |
| Category C                             |    NULL |    NULL |    NULL |
| Category C                             |    NULL |    NULL |    NULL |
| Category C                             |    NULL |    NULL |  102040 |
| Category C                             |    NULL |    NULL |    NULL |
| Category C                             |    NULL |    NULL |    NULL |
| Category C                             |   75000 |    NULL |    NULL |
| Category C                             |   26000 |    NULL |    NULL |
+----------------------------------------+---------+---------+---------+

我希望将每个类别的spend_1,spent_2,spent_3中的所有值相加,最终得到如下表格:

+---------------------------------------------+-----------------------+
| name                                        | total_spent           |
+---------------------------------------------+-----------------------+
| Category A                                  | sum of all occurences |
+---------------------------------------------+-----------------------+
| Category B                                  | sum of all occurences |
+---------------------------------------------+-----------------------+
| Category C                                  | sum of all occurences |
+---------------------------------------------+-----------------------+

1 个答案:

答案 0 :(得分:2)

我不知道这是否是最佳方式,但你可以试试这个:

SELECT name, SUM(
IFNULL(spent_1, 0) + 
IFNULL(spent_2, 0) + 
IFNULL(spent_3, 0)) total_spent 
from tableName
group by name

作为替代方案,您可以使用COALESCE(ANSI标准)而不是IFNULL(不是):

SELECT name, SUM(
COALESCE(spent_1, 0) + 
COALESCE(spent_2, 0) + 
COALESCE(spent_3, 0)) total_spent 
from tableName
group by name