It seems to be possible to count a single entity using
(COUNT(DISTINCT ?x) as ?count)
and for the number of distinct tuples for all variables in the query using
(COUNT(DISTINCT *) as ?count)
However, I cannot figure out how to count specific (distinct) tuples. Something like
(COUNT(DISTINCT ?a ?b ?c) as ?count)
does not seem to work. Am I doing it wrong or is this really not allowed in SPARQL 1.1? Or is it supposed to work and just not supported in Sesame 2.6.0 which I am using for testing this?
Make sure that your intermediate result only contains the three variables ?a ?b ?c
that you're interested in.
One way of doing this is to use a subquery. The subquery projects only the three desired variables. Something like this:
SELECT (COUNT(*) AS ?count) {
SELECT DISTINCT ?a ?b ?c {
…
}
}
(I'm not sure whether Sesame supports subqueries.)
Another way is to simply make sure that your query only contains the three variables. If you need more variables inside the query, you may be able to replace them with blank nodes. Blank nodes in SPARQL graph patterns work like “anonymous variables”. There are some funny scoping issues with this though, so the subquery approach is probably better.