简体   繁体   English

如何在JPQL中通过查询来计算一个组返回的所有组?

[英]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? 如何计算不使用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? JPQL有什么关系吗?

Derived tables are not supported in JPQL, so, the best way to do it is to run a native SQL query. JPQL不支持派生表,因此,最好的方法是运行本机SQL查询。

After all, there is a very good reason why both JPA and Hibernate offer support for SQL queries , don't you agree? 毕竟, 有一个很好的理由说明JPA和Hibernate都支持SQL查询 ,您不同意吗?

in oracle you can this hql 在甲骨文你可以这个HQL

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

but dose not work in postgres 但是在postgres中不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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