Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL @Query causing "unexpected token: ("

I have following method defined:

@Query("SELECT AVG(total) FROM (SELECT COUNT(t.name) total FROM DataTable t GROUP BY DATE(actiontime)) result")
Long countAvg();

However it causes this error:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected     token: ( near line 1, column 24 [SELECT AVG(total) FROM (SELECT     COUNT(t.name) total FROM backend.DataTable t GROUP BY     DATE(actiontime)) result]

But following SQL works fine:

SELECT AVG(total) FROM (SELECT COUNT(NAME) total FROM DATA_TABLE GROUP BY DATE(actiontime)) result 

If i understand correctly, JPQL has problems with subquery. How should i create this kind of query then?

like image 648
damnputer Avatar asked Apr 16 '26 12:04

damnputer


1 Answers

I don't think jpql supports subselect with from clause. As per jpa docs

Subqueries are restricted to the WHERE and HAVING clauses in this release. Support for subqueries in the FROM clause will be considered in a later release of the specification.

You can use nativeQuery = true in the @Query annotation and run it as native query instead or rewrite the query if possible.

like image 184
Madhusudana Reddy Sunnapu Avatar answered Apr 20 '26 16:04

Madhusudana Reddy Sunnapu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!