且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对没有子查询的同一列使用 Max 和 Sum -Mysql

更新时间:2022-12-12 09:48:34

原因是 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.