variable 'x' in projection not present in GROUP BY

∥☆過路亽.° 提交于 2020-04-16 02:23:09

问题


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.


回答1:


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 


来源:https://stackoverflow.com/questions/34033021/variable-x-in-projection-not-present-in-group-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!