gpt4 book ai didi

mysql - 在 `select` 的情况下,可以在 `group by` 中使用多个聚合函数吗?

转载 作者:行者123 更新时间:2023-11-29 09:30:07 26 4
gpt4 key购买 nike

这是表格:

enter image description here

这是我的查询

select sum(if(customer_pref_delivery_date = min(order_date), 1, 0)) immidiate_percentage
from Delivery
group by customer_id;

然后我收到错误

Invalid use of group function

忽略我逻辑上想做的事情,我想知道为什么会出现此错误?当我从 select 中删除 sum 时,它会起作用,所以我想在 sql 中可能只有一个聚合函数(例如 min 在这种情况下)在执行 group by 时是否允许在 select 中使用?这是真的吗?

最佳答案

您不能在选择列表中嵌套聚合。
如果您想获取每个客户的订单需要在同一天交付的订单的百分比,请执行以下操作:

select customer_id, 
100.0 * avg(customer_pref_delivery_date = order_date) immediate_percentage
from Delivery
group by customer_id;

请参阅demo .
结果:

| customer_id | immediate_percentage |
| ----------- | -------------------- |
| 1 | 0 |
| 2 | 50 |
| 3 | 50 |
| 4 | 100 |

关于mysql - 在 `select` 的情况下,可以在 `group by` 中使用多个聚合函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58901864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com