We can summarize or aggregate data using an aggregate function. For example,
SELECT AVG(a.balance) from Account a
will return the average balance over all accounts. The argument to AVG
must be numeric. Note that the return type of the AVG
function is Double
. As a single value is returned we execute the query using the Query.getSingleResult()
method.
The remaining aggregate functions provided by JPQL are listed below.
MAX
returns the maximum value of the group given an argument which must correspond to an orderable state-field type. Valid orderable state-field types are numeric types, string types, character types, or date types. The MAX
return type is equal to the type of the state-field argument.
MIN
returns the minimum value of the group given an argument which must correspond to an orderable state-field type. The MIN
return type is equal to the type of the state-field argument.
SUM
returns the sum of values of the group given a numeric argument. The return type...