JPQL queries can select aggregate data as well as objects.
JPQL includes the
min, max,
avg, and count aggregates. These
functions can be used for reporting and summary queries.
The following query will return the average of all the prices of all the magazines:
EntityManager em = ...
Query q = em.createQuery ("select avg(x.price) from Magazine x");
Number result = (Number) q.getSingleResult ();
The following query will return the highest price of all the magazines titled "JDJ":
EntityManager em = ...
Query q = em.createQuery ("select max(x.price) from Magazine x where x.title = 'JDJ'");
Number result = (Number) q.getSingleResult ();