开发者

SQL: how to get a weighted count using GROUP BY?

开发者 https://www.devze.com 2023-01-17 22:24 出处:网络
How can I make a query like: select myvalue, count() as freq fro开发者_Python百科m mytable group by myvalue;

How can I make a query like:

select myvalue, count() as freq fro开发者_Python百科m mytable group by myvalue;

But where freq sums values from a weight column instead of counting each row as 1?

I'm using MySQL 5.1.


You want to use the Sum aggregate function to handle this.

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

Should do the trick!


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


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


use sum(weight) as freq in your select query

0

精彩评论

暂无评论...
验证码 换一张
取 消