Aggregate functions cannot be nested, primarily because the value of the inner aggregate is not known at the time 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 program variable, so as to use it in computing an aggregate over a subsequent group.