简体   繁体   中英

How to count all groups returned by a group by query in JPQL?

If I have a group by query

Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...

How can I count the number of rows not using NativeQuery? I need to do something like this:

select count(*) from
(Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...)

Is there any way to do with JPQL?

Derived tables are not supported in JPQL, so, the best way to do it is to run a native SQL query.

After all, there is a very good reason why both JPA and Hibernate offer support for SQL queries , don't you agree?

in oracle you can this hql

Select sum(count(*) - count(*) + 1)
from Entity e
group by e.field1, e.field2...

but dose not work in postgres

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM