Question

I'm building a web application that has a search engine. I'm using NetBeans ( Java ) and SQL databases.

I'm trying to (group by) the results of a search engine. So I don't get duplicate results and use COUNT to rank the results. I'm having a problem writing it.

Query query = em.createQuery("select s.name from Searchresult s");

this WORKS for getting the name and this one works as well for grouping the results by name

Query query = em.createQuery("select s.name from Searchresult s group by s.name");

but this one doesn't

Query query = em.createQuery("select s.name, count(s.name) from Searchresult s group by s.name"); 

It doesn't work whenever i'm trying to add any other field (having more than one)

Query query = em.createQuery("select s.name, s.description from Searchresult s group by s.name");

^ so this one doesn't work too

How can I write a query in that gets more than one field (name, description, url) and get the count(name) and group by the result by names?

Was it helpful?

Solution

Duplicate of: GROUP BY without aggregate function

If you have a group by clause, only those fields in the group by and aggregate functions can be returned.

For example your second query:

select s.name, s.description from Searchresult s group by s.name

If you are collapsing all the records by s.name, what s.descriptions should the RDBMS return? There could be many s.decsription all with the same s.name.

Decide which you want back with an aggregate function:

select s.name, max(s.description) from Searchresult s group by s.name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top