Search code examples
javaselectrdfsparqlsesame

variable 'x' in projection not present in GROUP BY


I want to count municipalities and what I have in parentheses work. However, I want to get another variable too, but when I am adding it to SELECT, I am getting:

org.openrdf.query.MalformedQueryException: variable 'region_name' in projection not present in GROUP BY.

In my query I have:

"SELECT ?region_name (COUNT(?municipality) AS ?count) " +

What am I missing?

Notice that I do not have anything like GROUP anywhere in my project, I think it is happening internally of Sesame.

I just saw that if I remove (COUNT...), I am getting the names as expected.


Solution

  • In SPARQL, every query that uses an aggregate function (such as COUNT, SUM, SAMPLE, etc.) always uses a grouping. Even if you do not explicitly specify a GROUP BY clause in your query, it uses the 'default grouping' (that is, a single group to which all solutions belong).

    In a SPARQL query which uses aggregates, non-aggregated variables (such as ?region_name) may not be projected in the SELECT clause, unless they are explicitly added to the grouping.

    The fix is to add an explicit GROUP BY to your query:

    SELECT ?region_name (COUNT(?municipality) AS ?count)
    WHERE {
      ... 
    }
    GROUP BY ?region_name