Search code examples
sparqlaggregationallegrograph

Is it possible to aggregate over two resources in SPARQL?


The challenge

In my triple store, I have a resource type Email. Resources of that Email type have a sender and one or more recipients assigned.

Let the notation "A->B" be defined as "A is the sender of an email for which B is one of the recipients".

Now, I would like to know the following: How many times has person A sent an email to person B? Note that the role of A and B is important, i.e. A->B is different from B->A.

The malformed SPARQL query

In order to answer this question for all available pairings, I wrote this SPARQL (1.1) query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX email: <http://example.com/schema/for/email/>

SELECT ?sender ?recipient (COUNT(?sender ?recipient) AS ?count)
WHERE {
  ?sender rdf:type foaf:Person.
  ?recipient rdf:type foaf:Person.

  ?e rdf:type email:Email.
  ?e email:sender ?sender.
  ?e email:recipient ?recipient.
}
GROUP BY ?sender ?recipient

This does not work, AllegroGraph gives me the error MALFORMED QUERY: Line 5, Found ?recipient (of type varname) - line 5 being the SELECT clause. I tried some other variants, altering the SELECT and/or GROUP BY clause, but without success.

I am not sure whether aggregates on more than one resource are allowed. In the SPARQL 1.1 docs and a related SO question (Counting in SPARQL) I only found aggregates based on one resource.

So finally, here is my question: Is it possible to aggregate over sender AND recipient?


Solution

  • Try replacing (COUNT(?sender ?recipient) AS ?count) with (COUNT(?e) AS ?count).

    That should work because you want to count the number of emails (?e) that match your criteria.