Search code examples
sparqlrdfallegrographnamed-graphs

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)
  }
}
  • I realize that COUNT can't be in the WHERE, and it can't take variables.

Solution

  • 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