The reason is that SUM () and MAX () are cumulative functions, and these functions return results by grouping their inputs through a table. When applying one aggregate function over another, it is similar to grouping data by the result set of another grouping function, and this is not allowed in SQL.
But in SQL Server since the 2008 version, a new OVER () clause has appeared so that we can specify the grouping criteria in this above section for this particular column.
In the case of DISTINCT, which does not need any grouping, and it pulls out a separate set of records over which we apply the aggregate function. So it will work.
Unnikrishnan r
source share