Unfortunately, in JPQL, nested selects are not permitted in FROM
clause. They are allowed only in SELECT
and WHERE
.
You have two approaches available.
(1) Use findAVGPositions
and calculate sum in Java
findAVGPositions().stream().reduce(0f, Float::sum);
(2) Use a native SQL query
@Query(value = "SELECT SUM(averages) " +
"FROM (SELECT AVG(p.quantity) AS averages " +
"FROM position p " +
"GROUP BY client_id) AS averages_select",
nativeQuery = true)
public Float findSumAVGPositions();
Depending on the database you use, AS averages_select
alias might be needed or not (PostgreSQL requires it, even if it's not used).
Please, pay attention to use the correct names of a database table for Position
entity and a database column for a foreign key client.id
.
In my example, I assumed a standard mapping convention: position
as a table name, and client_id
as a foreign key column.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…