I have the following query:
SELECT ?tag WHERE {
?r ns9:taggedWithTag ?tagresource.
?tagresource ns9:name ?tag
}
LIM
Using COUNT(), MIN(), MAX(), SUM(), AVG() with GROUP BY can produce summary values for groups of triples. Note, these patterns might be specific to SPARQL 1.1.
For example, this one can sum the ?value for each ?category,
SELECT ?category (SUM(?value) as ?valueSum)
WHERE
{
?s ?category ?value .
}
GROUP BY ?category
This one can count the number of uses for predicate ?p,
SELECT ?p (COUNT(?p) as ?pCount)
WHERE
{
?s ?p ?o .
}
GROUP BY ?p
These examples are inspired by material from Bob DuCharme (2011), "Learning SPARQL". O’Reilly Media, Sebastopol, CA, USA; see http://www.learningsparql.com/
To avoid the error "Bad aggregate" upon using GROUP BY: