AGGREGATE statement
- Last Updated: February 11, 2026
- 2 minute read
- OpenEdge
- Version 13.0
- Documentation
Calculates a single aggregate value for a collection of rows on the database and assigns it to a variable. Because the operation executes server-side, you may see a performance improvement compared to doing the same operation client-side using the ACCUMULATE statement and ACCUM function.
Syntax
|
- aggregate-target-var
- The name of a field or variable to set with the aggregate value. The value returned is numeric, therefore a DECIMAL variable holds any value returned. You may use an INTEGER or INT64 variable, but the value returned is rounded.
- aggregate-operation
- Valid values are
COUNT,TOTALandAVERAGE.COUNT- Counts the number of rows of field that satisfy the where-clause, if specified. Unknown (?) values are included in the count.TOTAL- Sums the values of field in all rows that satisfy the where-clause, if specified. field must be a field of some numeric data type. Unknown (?) values are not included in the total.AVERAGE- Calculates the average of all values of the field that satisfy the where-clause, if specified. field must be a field of some numeric data type. When computing the average (total/count), unknown (?) values are included in the count, but not the total. If the query returns no records for theAVERAGEcalculation, theAGGREGATEstatement returns the Unknown (?) value.
- field
- The field name of a table you want to get the aggregate for. The field name
can be qualified with the table name (for example,
TableName.FieldName). - FOR table-name [ where-clause ]
- Defines the table name of the field and an optional where-clause for limiting the range of records
to be used for the aggregate. If no where-clause is specified, then all values are included.
If field is also qualified with a table name, then the table buffers must be the same.
Examples
This example code counts the number of records in the
Customer table:
|
This example code counts the number of customers in Boston:
|
This example code calculate the average balance for customers in
Chicago:
|
This example code sums the customer balances for customers in Los
Angeles:
|
Notes
- The
AGGREGATEstatement accesses all records usingNO-LOCK. - The
AGGREGATEstatement cannot be inside aFORloop for the same buffer. - If the session has a record in scope, then that record is no longer in scope
after the
AGGREGATEstatement executes. Therefore, you should not use the same buffers inAGGREGATEstatements that are used in other parts of the application. - The
AGGREGATEstatement is not limited to network clients. It can also be used with self-service clients. - Query information logging is available
for the
AGGREGATEstatement. Information is logged at level 3 (Verbose). - The following limitations apply to the
AGGREGATEstatement:- Can only be used with a single aggregate on a single table.
- Is not supported with the
TENANT-WHEREclause for multi-tenant tables.