示例数据
+----+------------+-------+------------+
| ID | first_name | sales | sale_date |
+----+------------+-------+------------+
| 1 | Lindsey | 32.02 | 2007-03-12 |
| 2 | Nicole | 26.53 | 2007-03-12 |
| 3 | Britney | 11.25 | 2007-03-12 |
| 4 | Ashley | 18.96 | 2007-03-12 |
| 5 | Lindsey | 9.16 | 2007-03-11 |
| 6 | Nicole | 1.52 | 2007-03-11 |
| 7 | Britney | 43.21 | 2007-03-11 |
| 8 | Ashley | 8.05 | 2007-03-11 |
| 9 | Lindsey | 17.62 | 2007-03-10 |
| 10 | Nicole | 24.19 | 2007-03-10 |
| 11 | Britney | 3.40 | 2007-03-10 |
| 12 | Ashley | 15.21 | 2007-03-10 |
| 13 | Lindsey | 0.00 | 2007-03-09 |
| 14 | Nicole | 31.99 | 2007-03-09 |
+----+------------+-------+------------+
我试图找到这个女孩中最大的总销售产品
我可以使用此查询找到最大的总销售额
select first_name, sum(sales) as total
from cookie_sales
group by first_name
order by total desc limit 1;
我想知道有没有办法使用聚合 max
函数找到相同的值
如果我使用这样的东西
select first_name, max(sum(sales)) from cookie_sales group by first_name;
我会收到 1111 Mysql 错误(无效使用组函数)
有办法吗?
最佳答案
您最初在问题中编写的查询可能是最好的方法。我不明白这样做有什么问题。这里不需要使用子查询。
关于mysql - 使用组合 sum 和 max 查找最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17732257/