Informix Error -8105: Aggregates may not be used within another aggregate. Nor may aggregates be used within the WHERE clause of another aggregate.
Cause and resolution
Aggregates may not be used within another aggregate. Nor may aggregates be used within the WHERE clause of another aggregate.
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.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-8105 fires when an aggregate function is nested inside another aggregate, or inside another
aggregate's WHERE clause — per the official guidance, this isn't supported because the
inner aggregate's value isn't known while the outer one is still accumulating.
- An aggregate nested inside another aggregate, per the official guidance — the first named case.
- An aggregate used inside another aggregate's
WHEREclause, per the official guidance — the second named case.
Solutions / Resolution
- Rewrite aggregate expressions to refer only to columns and simple expressions on columns, per the official guidance.
- In an
AFTER GROUP OFclause, save an earlier group's aggregate value in a variable, per the official guidance, to use it when computing an aggregate over a subsequent group.
Examples
A nested aggregate
PRINT SUM(AVG(order_total))
-- -8105: aggregates cannot nest
Corrected (using a variable across groups)
AFTER GROUP OF customer_id
LET prev_avg = running_avg
LET running_avg = AVG(order_total)
Diagnostic Checks
- Check every aggregate expression for a nested aggregate call, or an aggregate inside
another aggregate's
WHEREclause, and rewrite using columns/simple expressions or an intermediate variable.
Related Errors / Related Topics
- -8104 — "Group aggregates can only be used in an AFTER GROUP OF clause." A related aggregate-usage restriction, about clause placement rather than nesting.
An aggregate is nested inside another aggregate (or its WHERE clause) — rewrite using
columns/simple expressions, or a variable carried across groups.