对没有子查询的同一列使用 Max 和 Sum -Mysql [英] Using Max and Sum for the same column without subquery -Mysql

查看:70
本文介绍了对没有子查询的同一列使用 Max 和 Sum -Mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Mysql 新手,我想同时使用 sum(Max(column)) ?

I am new to Mysql,I wanted to use both sum(Max(column)) ?

我不知道为什么它无法使用?

I don't know why its impossible to use?

而 count(Distinct(column)) 是可能的,即一起使用两个聚合函数

whereas count(Distinct(column)) is possible i.e using two aggregate functions together

推荐答案

原因是 SUM() 和 MAX() 都是聚合函数,这些函数通过在表中对它们的输入进行分组来返回结果.将一个聚合函数应用于另一个聚合函数时,就像将数据分组到另一个分组函数的结果集上一样,这在 SQL 中是不允许的.

The reason is SUM() and MAX() both are aggregate functions and these functions returning results by grouping their inputs across the table . While applying one aggregate function over another, it is something like grouping data over the result set of another grouping function and that is not allowed in SQL.

但是在 SQL Server 中,从 2008 版本开始引入了一个新的 OVER() 子句,以便我们可以在该 over 子句中为该特定列指定分组条件.

But in SQL Server, from 2008 version onwards introduced a new clause OVER() so that we can specify the grouping criteria in that over clause for that particular column.

在不需要任何分组的 DISTINCT 的情况下,它会提取我们正在应用聚合函数的不同记录集.这样就可以了.

In the case of DISTINCT which does not need any grouping and it pull the distinct record set over which we are applying the aggregate function. So that will work.

这篇关于对没有子查询的同一列使用 Max 和 Sum -Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆