Landon Cheek
2012-06-08 16:46:40 UTC
I am having a very difficult time trying to figure out how to use nested
aggregate functions in HQL.
Let me give some domain class context:
Person {
static hasMany = [events: Event]
}
Event {
static belongsTo = [person: Person]
Date timestamp
}
Specifically, what I'm trying to do in my HQL query is get the average
number of events per day, per person.
Here's the HQL query I initially tried to use:
select avg ( ( select count(*) from Event event where event.person = person
group by event.person, day(event.timestamp), month(event.timestamp),
year(event.timestamp) ) ), person
from Person person
group by person
The subquery to get the event counts (which I use for the average) seems to
be what is giving me grief. The error I get when I try and execute the query
is "org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: query",
which makes sense because I'm fairly certain that actually is invalid
syntax. My issue is that there doesn't seem to be any other way to construct
the query to get out what I want, at least not in HQL.
In SQL, what I would normally do is put the subquery in the 'from' clause,
but HQL specifically doesn't allow this for some reason that is not
fathomable to me.
I'm at a loss. Any help would be greatly appreciated.
--
View this message in context: http://grails.1312388.n4.nabble.com/HQL-Nested-Aggregate-Functions-tp4629813.html
Sent from the Grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
aggregate functions in HQL.
Let me give some domain class context:
Person {
static hasMany = [events: Event]
}
Event {
static belongsTo = [person: Person]
Date timestamp
}
Specifically, what I'm trying to do in my HQL query is get the average
number of events per day, per person.
Here's the HQL query I initially tried to use:
select avg ( ( select count(*) from Event event where event.person = person
group by event.person, day(event.timestamp), month(event.timestamp),
year(event.timestamp) ) ), person
from Person person
group by person
The subquery to get the event counts (which I use for the average) seems to
be what is giving me grief. The error I get when I try and execute the query
is "org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: query",
which makes sense because I'm fairly certain that actually is invalid
syntax. My issue is that there doesn't seem to be any other way to construct
the query to get out what I want, at least not in HQL.
In SQL, what I would normally do is put the subquery in the 'from' clause,
but HQL specifically doesn't allow this for some reason that is not
fathomable to me.
I'm at a loss. Any help would be greatly appreciated.
--
View this message in context: http://grails.1312388.n4.nabble.com/HQL-Nested-Aggregate-Functions-tp4629813.html
Sent from the Grails - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email