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

发布于 2024-09-25 14:17:22 字数 206 浏览 8 评论 0原文

我如何进行如下查询:

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

但是其中 freqweight 列中的值进行求和,而不是将每一行计为 1?

我正在使用 MySQL 5.1。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活雷疯 2024-10-02 14:17:22

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

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

应该做的伎俩!

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!

凤舞天涯 2024-10-02 14:17:22
select myvalue, sum(weight) as freq from mytable group by myvalue;
select myvalue, sum(weight) as freq from mytable group by myvalue;
々眼睛长脚气 2024-10-02 14:17:22
select myvalue, SUM(weight) as freq from mytable group by myvalue;
select myvalue, SUM(weight) as freq from mytable group by myvalue;
一杯敬自由 2024-10-02 14:17:22

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

use sum(weight) as freq in your select query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文