views:

42

answers:

3

I have a Vote domain class from my grails application containing properties like article_id and note

I want to HQL query the Vote domain class in order to retrieve the 5 best rated articles having at least 10 votes.

I tried :

SELECT v.article_id, avg(v.note), count(*) FROM vote v where count(*) >= 10  group by v.article_id order by avg(v.note) desc limit 5;

But unfortunately the insertion of where count(*) >= 10 throws an error.

How can I do that in a simple way?

Thank you for your help.

A: 

doesn't the following work ?

SELECT v.article_id, avg(v.note), count(*) as count 
FROM vote v 
where count >= 10  
group by v.article_id 
order by avg(v.note) desc limit 5;

Later edit: Try this

SELECT v.article_id, avg(v.note) 
from vote v 
group by v.article_id 
having count(*) >= 10
order by avg(v.note) desc limit 5;
Toader Mihai Claudiu
Not 100% sure but I don't think you can use the count alias in a WHERE in the same query
Lauri Lehtinen
Not working at all. It seems that we Cannot use count alias in a WHERE
fabien7474
ah .. i was afraid of that :(. having the the way to go then.
Toader Mihai Claudiu
A: 

Not being able to test it, I'm reluctant to try to guess how the final query would look like, but HAVING COUNT(*) >= 10 might be something to look at.

http://www.sqlcommands.net/sql+having/

Lauri Lehtinen
A: 

Try that:

SELECT v.article_id, avg(v.note),
FROM vote v 
GROUP BY v.article_id
HAVING count(*) >= 10
OREDER BY avg(v.note) desc limit 5;
silk