我在下面有这个示例 MySQL 表:
date |name |invoice | quantity |
-------------------------------
2012-10-02 01:00|aaaa |101 | 1
2012-10-02 01:30|aaaa |102 | 2
2012-10-02 01:45|aaaa |102 | 3
我想对日期进行GROUP BY
并得到类似的结果:
date | name |invoice | freq_name | freq_quantity |
--------------------------------------------------
2012-10-02| aaaa |101 |1 | 1
2012-10-02| aaaa |102 |2 | 5
我已尝试执行如下聚合:
SELECT
date,
name,
invoice,
SUM(quantity) AS freq_quantity,
SUM(name) AS freq_name
FROM table
GROUP BY date, name, invoice
freq_quantity
字段正确,而 freq_name
字符串字段不正确。
最佳答案
您不需要对名称求和
,您需要对它们计数
:
SELECT
date,
name,
invoice,
SUM(quantity) AS freq_quantity,
COUNT(*) AS freq_name -- Here!
FROM table
GROUP BY date, name, invoice
关于mysql - 获取 MySQL 中字符串字段的 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805965/