How to do a count and select variables in Sparql

500 Views Asked by At

Is there a way to select variables and get a count of the total variables in one Sparql query ? I want to return ?s and get a count of all the ?s matches

I tried variations of the query below but I keep getting errors

SELECT (COUNT(*) as ?cnt) ?s { ?s a <http://foo.org/test>  }  
1

There are 1 best solutions below

2
On BEST ANSWER
SELECT (COUNT(*) as ?cnt) (sample(?s) as ?sample)
{ ?s a <http://foo.org/test>  } 

COUNT means the query is an aggregate query so there are many ?s. SAMPLE picks one - which one is not defined.

If you want all the ?s and the COUNT, you'd normally count in your application - to do in the query is a bit odd, you need to ask twice:

SELECT ?cnt ?s 
{ { SELECT (COUNT(*) as ?cnt) { ?x a <http://foo.org/test> } }
   UNION { ?s a <http://foo.org/test> }
}