Aggregate functions cannot be nested, primarily because the value of the inner aggregate is not known while the outer aggregate is being accumulated. Rewrite aggregate expressions to refer only to columns and simple expressions on columns. In an AFTER GROUP OF clause, you can save the aggregate value from one group of rows in a variable in order to use it to compute an aggregate over a subsequent group.