Informix Error -231: Cannot perform aggregate function with distinct on expression.
Cause and resolution
Cannot perform aggregate function with distinct on expression.
This statement selects DISTINCT (expression) within an aggregate function. This action is not supported. Select the DISTINCT value and other columns into a temporary table; then select ALL from that table applying the aggregate function.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-231 is a specific, well-defined restriction with a documented workaround: DISTINCT inside an
aggregate function only works against a simple column reference, not a computed expression.
DISTINCTapplied to a complex expression inside an aggregate function —COUNT(DISTINCT col1 + col2)orSUM(DISTINCT some_expr), rather than a plain column reference.- Assuming
DISTINCTworks the same way on expressions as on simple columns within an aggregate — a reasonable assumption that just isn't supported here. - SQL ported from another database system that permits
DISTINCTon arbitrary expressions inside aggregates directly.
Solutions / Resolution
- Select the
DISTINCTexpression into a temporary table first, then apply the aggregate function withALLagainst that table — the documented workaround, per the official guidance. - Alternatively, use a subquery or derived table to perform the
DISTINCTstep separately before aggregating, functionally equivalent to the temp-table approach.
Examples
The disallowed pattern
SELECT COUNT(DISTINCT price * quantity) FROM order_items;
-- -231: DISTINCT on a computed expression inside an aggregate
The documented workaround
SELECT DISTINCT price * quantity AS line_total INTO TEMP tmp_totals
FROM order_items;
SELECT COUNT(*) FROM tmp_totals;
Using a derived table instead
SELECT COUNT(*) FROM (
SELECT DISTINCT price * quantity AS line_total FROM order_items
) t;
Diagnostic Checks
- Review the aggregate function for a
DISTINCTapplied to something other than a simple column reference — that's always the trigger for this specific error.
Related Errors / Related Topics
- -201 — "A syntax error has occurred." The general SQL-parsing-error family this fits into.
Compute the expression first (into a temp table or a derived table), then apply DISTINCT and
the aggregate separately — that's the standard, documented pattern for this restriction.