HQL supports a range of aggregate methods, similar to SQL. They work the same way in HQL as in SQL and following is the list of the available functions:
S.N. | Functions | Description |
1 | avg(property name) | The average of a property's value |
2 | count(property name or *) | The number of times a property occurs in the results |
3 | max(property name) | The maximum value of the property values |
4 | min(property name) | The minimum value of the property values |
5 | sum(property name) | The sum total of the property values |
The
distinct keyword only counts the unique values in the row set. The following query will return only unique count:
String hql ="SELECT count(distinct E.firstName) FROM Employee E";
Query query =session.createQuery(hql);
List results =query.list();