假设我有一张看起来像的表:
| C1 | C2 | C3 | C4 |
| AAAA | 11 | 14 | 12 |
| AAAA | 21 | 04 | 31 |
| AAAA | 17 | 24 | 13 |
| AAAA | 13 | 13 | 14 |
| AAAA | 11 | 23 | 12 |
| AAAA | 19 | 21 | 15 |
| BBBB | 11 | 11 | 19 |
| BBBB | 16 | 11 | 18 |
| BBBB | 17 | 14 | 13 |
| BBBB | 13 | 12 | 13 |
| BBBB | 12 | 21 | 11 |
| BBBB | 12 | 21 | 13 |
这就是我想要的样子:
| C1 | C2 | C3 | C4 |
| AAAA | 92 | 99 | 97 |
| BBBB | 81 | 90 | 87 |
我知道我可以这样做:
SELECT SUM( C2 ), SUM( C3 ), SUM( C4 )
FROM mytable;
问题在于它需要整个列的全部总和。有没有办法让它在C1中的内容方面做到这一点?
答案 0 :(得分:1)
您需要对查询进行分组 像这样
SELECT SUM( C2 ), SUM( C3 ), SUM( C4 )
FROM mytable GROUP BY C1
答案 1 :(得分:1)
你错过了一个小组。
SELECT C1, SUM(C2), SUM(C3), SUM(C4)
FROM mytable GROUP BY C1
如果未指定group by,则它将对整个表进行求和。