virtuoso-opensource icon indicating copy to clipboard operation
virtuoso-opensource copied to clipboard

HAVING unable to filter on aggregates

Open knoan opened this issue 11 years ago • 4 comments

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

knoan avatar Oct 31 '14 08:10 knoan

I have been able to recreate this error locally and reported to development to look into ....

HughWilliams avatar Oct 31 '14 11:10 HughWilliams

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.

knoan avatar Apr 28 '15 07:04 knoan

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)

HughWilliams avatar Apr 28 '15 08:04 HughWilliams

Spot on…

knoan avatar Apr 28 '15 09:04 knoan