HAVING unable to filter on aggregates
select ?s (count(?o) as ?count) where {
?s ?p ?o
} group by ?s having (?count > 3)
Fails unexpectedly with
Virtuoso 37000 Error SP031: SPARQL compiler: Variable ?count is used in the result set outside aggregate and not mentioned in GROUP BY clause
- Fine without HAVING clause
- Fine with Sesame 2.7 / Fuseki 1.1
I have been able to recreate this error locally and reported to development to look into ....
Not sure that my expectations are correct here… the ability to filter on projected aggregates is likely to be a (handy) extension of sesame/jena beyond what specified by the standard.
SPARQL 1.1 paragraph "18.2.4.2 HAVING" states that "expressions projected by the SELECT clause are not visible to the HAVING clause". In other words, variable ?count gets its value after filtering of grouped solutions by HAVING.
The right variant of the query would be
select ?s (count(?o) as ?count) from virtrdf: where { ?s ?p ?o } group by ?s having (count(?o) > 3)
Spot on…