Having trouble with Hibernate HQL query containing nested aggregate functions
Question
The following hql_query String works and delivers data records as expected. It gives me a list of lists, where the nested lists consist of a person pers and "the amount of persAccsBOs".
hql_query_string = ""
+ "select distinct pers, count(persAccsBOs) as anzBo from Person as pers"
+ " inner join pers.personenAttribute as persAttr inner join persAttr.pa_pk.attribut as pAttr"
+ " inner join pers.accounts as persAccs"
+ " inner join persAccs.berechtigungsobjekte as persAccsBOs"
+ " where pAttr.name='UID_Department'"
+ " and persAttr.wert='Sales (10100200)'"
+ " group by pers";
Now, what i want to achieve and fail with is to get the average value of all anzBo's. I tried the following query_string:
query_string = ""
+ "select avg(anzBo) from "
+ " (select distinct pers, count(persAccsBOs) as anzBo from Person as pers"
+ " inner join pers.personenAttribute as persAttr inner join persAttr.pa_pk.attribut as pAttr"
+ " inner join pers.accounts as persAccs"
+ " inner join persAccs.berechtigungsobjekte as persAccsBOs"
+ " where pAttr.name='UID_Department'"
+ " and persAttr.wert='Sales (10100200)'"
+ " group by pers)";
I get the error message:
unexpected token: select near line 1, column 25 [select avg(anzBo) from select distinct pers ...
I hope you got the clue what im trying to do. Can you help me to form the correct hql_string?
UPDATE:
If i try this:
query_string = ""
+ "select distinct pers, count(persAccsBOs) as anzBo, avg(anzBo) from Person as pers"
+ " inner join pers.personenAttribute as persAttr inner join persAttr.pa_pk.attribut as pAttr"
+ " inner join pers.accounts as persAccs"
+ " inner join persAccs.berechtigungsobjekte as persAccsBOs"
+ " where pAttr.name='UID_Department'"
+ " and persAttr.wert='Sales (10100200)'"
+ " group by pers";
i will get a java.lang.NullPointerException.
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow