SQL:如何使用GROUP BY获得加权计数?

时间:2010-09-29 16:40:04

标签: sql mysql frequency-analysis

如何进行如下查询:

select myvalue, count() as freq from mytable group by myvalue;

但是freqweight列的值求和而不是将每行计为1?

我正在使用MySQL 5.1。

4 个答案:

答案 0 :(得分:3)

您希望使用Sum聚合函数来处理此问题。

SELECT MyValue, Sum(weight) AS Total
FROM mytable
GROUP BY myvalue;

应该做的伎俩!

答案 1 :(得分:2)

select myvalue, sum(weight) as freq from mytable group by myvalue;

答案 2 :(得分:2)

select myvalue, SUM(weight) as freq from mytable group by myvalue;

答案 3 :(得分:1)

在您的选择查询中使用sum(weight) as freq