gpt4 book ai didi

mysql - MySQL 中的非聚合平均值?

转载 作者:行者123 更新时间:2023-11-29 02:33:15 27 4
gpt4 key购买 nike

有没有一种方法可以在不使用聚合函数的情况下计算子查询返回结果的平均值?

假设的例子:

我想为每个帐户选择一个月内的平均最大发票数。我希望能够做这样的事情:

SELECT
accounts.*,
**AVERAGE**(SELECT MAX(`amount`)
FROM invoices
WHERE invoices.account_id = accounts.id
GROUP BY invoices.month) as `average_max`

最佳答案

根据定义,平均是一种聚合函数,因为它需要对数据的聚合进行操作。

为了得到你想要的,我会这样做:

SELECT
accounts.*, avg_amount.amount AS avg_amount
FROM
accounts
LEFT JOIN (
SELECT account_id, AVG(amount) AS amount FROM (
SELECT month, account_id, MAX(amount) AS amount FROM invoices GROUP BY month, account_id
) AS max_amount USING(account_id)
) AS avg_amount ON (accounts.id = avg_amount.account_ID)

关于mysql - MySQL 中的非聚合平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9453578/

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