9me
9me

Reputation: 1138

Sparql BIND result of count WITH A VARIABLE

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

Upvotes: 1

Views: 1375

Answers (1)

A'B
A'B

Reputation: 535

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.

Upvotes: 9

Related Questions