user1387786
user1387786

Reputation: 302

JPA criteria query with subquery

I was wondering if it was possible to express the following query with jpa 2.0 criteria api.

simple_table:
user_name(varchar),
bytes_total(bigint),
time_total(bigint)

>select
   user_name, 
   sum(bytes_total),
   sum(bytes_total) * 100 / (select sum(bytes_total) from simple_table),
   sum(time_total),
   sum(time_total) * 100 / (select sum(time_total) from simple_table)
from simple_table
group by user_name

I have everything working except for the two sub-queries. I would like to know whether or not this is possible. Thanks.

Upvotes: 1

Views: 2114

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42114

That cannot be done. Subqueries cannot be used in SELECT clause. Same limitation is in JPQL queries. In JPA 2.0 specification this is expressed as follows:

Subqueries may be used in the WHERE and HAVING clause.

Upvotes: 1

Related Questions