Informix Error -595: Incorrect use of an aggregate function.
Cause and resolution
Incorrect use of an aggregate function.
An aggregate function was incorrectly used inside an SPL routine expression, as an argument to an iterator table function, or inside a check constraint. These types of expressions cannot process aggregate functions.
The following examples show the incorrect use of an aggregate function:
LET var = MAX(another_var) + 10; -- error
SELECT 1 FROM TABLE(FUNCTION(udr1(max(1)))); -- error
An SPL routine expression, or the expression in a check constraint, can refer to only a single value, so the use of an aggregate function is meaningless.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-595 fires when an aggregate function (SUM, COUNT, MAX, and similar) appears somewhere it
can't be evaluated — per the official guidance, inside an SPL routine expression, as an argument
to an iterator table function, or inside a CHECK constraint. Per the official guidance's
explanation, these contexts all deal with a single value at a time, so an aggregate — which is
defined over a group of rows — is meaningless there.
- An aggregate function used directly in an SPL routine expression (an
IFcondition, a variable assignment outside aSELECT INTO), per the official guidance. - An aggregate function passed as an argument to an iterator table function, per the official guidance.
- An aggregate function used inside a
CHECKconstraint's expression, per the official guidance — a check constraint evaluates against one row's values, not a group.
Solutions / Resolution
- Compute the aggregate via a
SELECT ... INTOfirst, in an SPL routine, then use the resulting single value in subsequent expressions:DEFINE v_total DECIMAL; SELECT SUM(order_total) INTO v_total FROM orders WHERE customer_id = p_customer_id; IF v_total > 1000 THEN ... - For a check constraint, express the rule in terms of the row's own columns only, per the
official guidance's underlying point — a check constraint can't reference an aggregate over
other rows at all; if the intended rule genuinely needs one, it isn't expressible as a
CHECKconstraint and needs a trigger or application-level enforcement instead. - For an iterator table function argument, compute the aggregate separately beforehand and pass the resulting scalar value in, rather than the aggregate expression itself.
Examples
The disallowed use inside an SPL routine
CREATE PROCEDURE flag_big_spenders(p_customer_id INT)
IF SUM(SELECT order_total FROM orders WHERE customer_id = p_customer_id) > 1000 THEN
...
END IF;
END PROCEDURE;
-- -595: aggregate used directly in an IF expression
Corrected — compute it first
CREATE PROCEDURE flag_big_spenders(p_customer_id INT)
DEFINE v_total DECIMAL;
SELECT SUM(order_total) INTO v_total FROM orders WHERE customer_id = p_customer_id;
IF v_total > 1000 THEN
...
END IF;
END PROCEDURE;
Diagnostic Checks
- Identify which of the three contexts (SPL expression, iterator table function argument, check constraint) the aggregate appears in, and restructure it to compute the aggregate separately first.
Related Errors / Related Topics
- -544 — "Cannot have aggregates within aggregates." A related aggregate-usage restriction, about nesting aggregates rather than using one in a context that can't evaluate aggregates at all.
Compute the aggregate via SELECT ... INTO first, then use the resulting scalar value — none of
the three restricted contexts can evaluate an aggregate directly.