Aries On The Cusp
Aries On The Cusp

Reputation: 419

How to do a count and select variables in Sparql

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>  }  

Upvotes: 0

Views: 480

Answers (1)

AndyS
AndyS

Reputation: 16700

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> }
}

Upvotes: 2

Related Questions