我有一个表,其中包含一个键列表,每个键都与一个数值相关联。 我想运行一个SQL查询来列出一列中的每个不同键以及在另一列中具有该不同键的所有值的总和。 此过程与首先选择个人相同
key | val 1 | 4 2 | 2 1 | 6 3 | 7 2 | 10会回来:
key | sum 1 | 10 2 | 12 3 | 7我有:
SELECT DISTINCT key as ukey, SUM(value) as sum from table但这不起作用。 有查询吗?
I have a table with a list of keys, each associated with a numeric value. I want to run an SQL query to list each distinct keys in one column and the sum of all values that have that distinct key in another. This process is identical to first selecting individual
key | val 1 | 4 2 | 2 1 | 6 3 | 7 2 | 10would return:
key | sum 1 | 10 2 | 12 3 | 7I have:
SELECT DISTINCT key as ukey, SUM(value) as sum from tableBut that doesn't work. Is there a query?
最满意答案
SELECT key, SUM(value) as sum FROM table GROUP BY key
这是mysql的参考:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
SELECT key, SUM(value) as sum FROM table GROUP BY key
Here is reference from mysql:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
更多推荐
发布评论