Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple aggregates in SPARQL
    primarykey
    data
    text
    <p>I have a triple store that contains mail archive data. So let's say I have a lot of persons (<code>foaf:Person</code>) that have sent (<code>ex:hasSent</code>) and received (<code>ex:hasReceived</code>) emails (<code>ex:Email</code>).</p> <p>Example:</p> <pre><code>SELECT ?person ?email WHERE { ?email rdf:type ex:Email. ?person rdf:type foaf:Person; ex:hasSent ?email. } </code></pre> <p>The same works for <code>ex:hasReceived</code>, of course. Now I would like to do some statistics and analytics, i.e. determine how many emails an individual has sent and received. Doing this for only one predicate is a simple aggregation:</p> <pre><code>SELECT ?person (COUNT(?email) AS ?count) WHERE { ?email rdf:type ex:Email. ?person rdf:type foaf:Person; ex:hasSent ?email. } GROUP BY ?person </code></pre> <p>However, I need need the number of received emails as well and I would like to do this without having to issue a separate query. So I tried the following:</p> <pre><code>SELECT ?person (COUNT(?email1) AS ?sent_emails) (COUNT(?email2) AS ?received_emails) WHERE { ?person rdf:type foaf:Person. ?sent_email rdf:type ex:Email. ?person ex:hasSent ?sent_email. ?received_email rdf:type ex:Email. ?person ex:hasReceived ?received_email. } GROUP BY ?person </code></pre> <p>This did not seem to be right, as the numbers for the emails sent vs. received were exactly the same. I assume this is because my SPARQL statement results in a cross product of all mails a person has ever sent and received, right?</p> <p>What do I need to do in order to get the statistics right on a per-individual basis?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload