Ziqi
Ziqi

Reputation: 2554

sparql how to count variable pairs

I have the following query that gets instances of a class and their label/names. I want to count how many total results there are. However, I do not know how to formulate the count statement.

select ?s ?l {
   ?s a <http://dbpedia.org/ontology/Ship> . 
   {?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
   union
   {?s <http://xmlns.com/foaf/0.1/name> ?l} 
}

I have tried

select ?s ?l (count (?s) as ?count) {
   ?s a <http://dbpedia.org/ontology/Ship> . 
   {?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
   union 
   {?s <http://xmlns.com/foaf/0.1/name> ?l} 
}

But that gives the counting for each ?s ?l pair, instead I need to know how many of the ?s ?l pairs there are. Or maybe I should not use count at all? As mentioned all I need to know is how many results in total a query returns (regardless of the hard limit that is put by the server, e.g., DBPedia returns a maximum of 50000 results for each query).

Any suggestions please?

Many thanks!

Upvotes: 3

Views: 531

Answers (1)

scotthenninger
scotthenninger

Reputation: 4001

To count the number of matches, use

SELECT (COUNT(*) AS ?count)
WHERE {
   ?s <http://www.w3.org/2000/01/rdf-schema#label> | <http://xmlns.com/foaf/0.1/name> ?l .
}

Note I'm using the property path "or" (|) to get the union of the properties.

Upvotes: 2

Related Questions