The group by clause by R4R Team

A query that returns aggregate values can be grouped by any property of a returned class or components:

select student.color, sum(student.weight), count(student)
from Student student
group by student.color
select zoo.id, avg(name), max(name)
from Zoo zoo join zoo.names name
group by zoo.id

A having clause is also allowed.

select student.color, sum(student.weight), count(student)
from Student student
group by student.color
having cat.color in (eg.Color.TABBY, eg.Color.BLACK)

SQL functions and aggregate functions are allowed in the having and order by clauses if they are supported by the underlying database (i.e., not in MySQL).

select student from Student student join student.kittens kitten group by student.id, student.name, student.other, student.properties
having avg(kitten.weight) > 100
order by count(kitten) asc, sum(kitten.weight) desc

Neither the group by clause nor the order by clause can contain arithmetic expressions. Hibernate also does not currently expand a grouped entity, so you cannot write group by cat if all properties of cat are non-aggregated. You have to list all non-aggregated properties explicitly.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!