不同的两列和第三列相加

时间:2013-07-02 15:51:33

标签: sql-server sum distinct

我有下一张表,名为:mytable

---------------------------------
|id |  date  | name|actionCount |
---------------------------------
|a1 |03.03.12| ba  |     12     |
|a3 |06.02.13| ba  |     7      |
|a1 |06.02.13| ca  |     2      |
|a3 |06.02.13| ba  |     9      |
|a1 |06.06.13| ba  |     2      |
|--------------------------------

我希望获得不同的ID和名称,并将它们计算在内。

所以我必须得到:

------------------------
|id | name|actionCount |
------------------------
|a1 | ba  |     14     |
|a3 | ba  |     16     |
|a1 | ca  |     2      |
------------------------

我试过了:

SELECT DISTINCT id,name,SUM(actionCount) FROM mytable GROUP BY actionCount

但我得到了:

Column 'mytable.id' is invalid in the select list because it is not contained in either
an aggregate function or the GROUP BY clause.

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:3)

你可以尝试这样......

SELECT id,name,SUM(actionCount) FROM mytable GROUP BY id,name