MySql Sql MAX 和 SUM 错误

发布于 2024-08-17 16:49:39 字数 194 浏览 3 评论 0原文

select sum(value) as 'Value',max(value)
from table_name where sum(value)=max(sum(value)) group by id_name;

错误是:组函数的使用无效(ErrorNr.1111)

有什么想法吗?

谢谢。

select sum(value) as 'Value',max(value)
from table_name where sum(value)=max(sum(value)) group by id_name;

The error is: Invalid use of group function (ErrorNr. 1111)

Any idea?

Thanks.

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

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

发布评论

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

评论(2

一念一轮回 2024-08-24 16:49:39

您可以尝试

SELECT Value, MXValue
FROM (
       select sum(value) as 'Value',max(value)  MXValue
       from table_name 
       group by id_name
     ) as t1
order by value desc
LIMIT 0,1

MySQL 论坛 :: 常规 :: 选择 MAX (SUM())

或者你可以尝试类似

SELECT  id_name,
        Value
FROM    (
            select id_name,sum(value) as 'Value'
            from table_name
            group by id_name
        ) t
WHERE   Value = (
                    SELECT TOP 1 SUM(Value) Mx 
                    FROM table_name
                    GROUP BY id_name 
                    ORDER BY SUM(Value) DESC
                )

Or 甚至使用 Inner join

SELECT  id_name,
        Value
FROM    (
            select id_name,sum(value) as Value
            from table_name
            group by id_name
        ) t INNER JOIN
        (
            SELECT TOP 1 SUM(Value) Mx 
            FROM table_name
            GROUP BY id_name 
            ORDER BY SUM(Value) DESC
        ) m ON Value = Mx

Can you maybe try

SELECT Value, MXValue
FROM (
       select sum(value) as 'Value',max(value)  MXValue
       from table_name 
       group by id_name
     ) as t1
order by value desc
LIMIT 0,1

From MySQL Forums :: General :: selecting MAX(SUM())

Or you could try something like

SELECT  id_name,
        Value
FROM    (
            select id_name,sum(value) as 'Value'
            from table_name
            group by id_name
        ) t
WHERE   Value = (
                    SELECT TOP 1 SUM(Value) Mx 
                    FROM table_name
                    GROUP BY id_name 
                    ORDER BY SUM(Value) DESC
                )

Or even with an Inner join

SELECT  id_name,
        Value
FROM    (
            select id_name,sum(value) as Value
            from table_name
            group by id_name
        ) t INNER JOIN
        (
            SELECT TOP 1 SUM(Value) Mx 
            FROM table_name
            GROUP BY id_name 
            ORDER BY SUM(Value) DESC
        ) m ON Value = Mx
远昼 2024-08-24 16:49:39

=max(sum(value)) 部分需要比较两个分组选择的结果,而不仅仅是一个。 (总和的最大值。)

不过,让我们退后一步:您实际上想要获取什么信息?因为表中的值的总和是唯一的;没有最小值或最大值(或者,根据您的观点,存在 - 该值是其自身的最小值和最大值)。您需要在其中应用一些进一步的标准,以使结果有意义,并且在此过程中,您可能需要使用某些标准进行联接或子选择。

The =max(sum(value)) part requires comparing the results of two grouped selects, not just one. (The max of the sum.)

Let's step back, though: What information are you actually trying to get? Because the sum of the values in the table is unique; there is no minimum or maximum (or, depending on your viewpoint, there is -- the value is its own minimum and maximum). You'd need to apply some further criteria in there for the results to be meaningful, and in doing so you'd probably need to be doing a join or a subselect with some criteria.

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