Jeff
Jeff

Reputation: 4003

Given a predicate, count number of objects for each subject?

I'm trying to write a SPARQL query that will return a subject and the number of objects it has for a particular predicate. Here's what I have, but it is not working.

SELECT ?quote (COUNT(?quotedBy) AS ?no) WHERE {
  ?quote <http://scta.info/property/quotationType> <http://scta.info/resource/quoteType/Biblical> . 
  ?quote <http://scta.info/property/quotedBy> ?quotedBy .
} 

Upvotes: 0

Views: 984

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85863

Your query isn't legal. If you check at sparql.org's query validator, you'll get the response:

Syntax error:

Non-group key variable in SELECT: ?quote

You need to group by one or more variables. In this case, you want to count the number of ?quotedBy values per value of ?quote, so you'd need to group by ?quote:

select ?quote (count(?quotedBy) AS ?no) where {
  ?quote <http://scta.info/property/quotationType> <http://scta.info/resource/quoteType/Biblical> . 
  ?quote <http://scta.info/property/quotedBy> ?quotedBy .
} 
group by ?quote 

Upvotes: 4

Related Questions