Neil Graham
Neil Graham

Reputation: 893

SPARQL: Number of Statements in each Named Graph

The following query returns the number of statements in a repository:

SELECT (COUNT(*) AS ?count) 
WHERE {
  ?s ?p ?o
}

Is there a way to return the number of statements for each Named Graph?

This following query does not work, only an example:

SELECT ?graphName ?count 
WHERE {
  GRAPH ?graphName { 
    ?s ?p ?o. 
    BIND(COUNT(?s ?p ?o.) AS ?count)
  }
}

Upvotes: 1

Views: 488

Answers (1)

TallTed
TallTed

Reputation: 9444

Just add a GROUP BY clause to your query --

SELECT ?graphName
       ( COUNT ( * ) AS ?count )
WHERE
  {
    GRAPH ?graphName
      {
        ?s ?p ?o
      }
  }
GROUP BY ?graphName

See the query and its live results on DBpedia

Upvotes: 3

Related Questions