Sparql BIND result of count WITH A VARIABLE

2019-03-04 19:05发布

问题:

Is there any way to bind the result of count to a variable? I've tried the following (which doesn't work):

SELECT ?totalSubject WHERE {
  ?s ?p ?o
  BIND(COUNT(?s) AS ?totalSubject)
}

回答1:

COUNT is an aggregate function and may be used only to define projected variables. To count all matches, your specific example should read:

SELECT ( COUNT(?s) AS ?totalSubject ) WHERE {

    ?s ?p ?o. 

}

However, aggregate functions are usually applied to groups of matches. For instance, to count subjects grouped by type:

SELECT ?t ( COUNT(?s) AS ?totalSubject ) WHERE {

     ?s a ?t.

} GROUP BY ?t

Be aware that when using aggregate functions your query is subject to some restrictions: selected variables must be either

  • simple variables included in GROUP BY; or
  • aggregates or constant values.


标签: sparql