Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

15.12. group by 節

集約値を返すクエリは、返されるクラスやコンポーネントのプロパティによってグループ化できます:
select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
select foo.id, avg(name), max(name)
from Foo foo join foo.names name
group by foo.id
having 節も使えます。
select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
having cat.color in (eg.Color.TABBY, eg.Color.BLACK)
基盤のデータベースがサポートしている場合、 havingorder by 節で SQL 関数と集約関数が使えます(例えば MySQL にはありません)。
select cat
from Cat cat
    join cat.kittens kitten
group by cat.id, cat.name, cat.other, cat.properties
having avg(kitten.weight) > 100
order by count(kitten) asc, sum(kitten.weight) desc
group by 節や order by 節に算術式を含むことができません。また、Hibernate は今のところグループエンティティを拡張しないため、cat の全てのプロパティが非集合体の場合、group by cat を書くことはできません。全ての非集合体のプロパティを明示的にリストする必要があります。