Search code examples
sqlmysqlfrequency-analysis

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


How can I make a query like:

select myvalue, count() as freq from 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.


Solution

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