aggregate-functions


Multiple aggregates in SPARQL


I have a triple store that contains mail archive data. So let's say I have a lot of persons (foaf:Person) that have sent (ex:hasSent) and received (ex:hasReceived) emails (ex:Email).
Example:
SELECT ?person ?email
WHERE {
?email rdf:type ex:Email.
?person rdf:type foaf:Person;
ex:hasSent ?email.
}
The same works for ex:hasReceived, 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:
SELECT ?person (COUNT(?email) AS ?count)
WHERE {
?email rdf:type ex:Email.
?person rdf:type foaf:Person;
ex:hasSent ?email.
}
GROUP BY ?person
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:
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
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?
What do I need to do in order to get the statistics right on a per-individual basis?
COUNT(?email1) isn't counting anything as ?email1 is undefined. Also, there is partial cross product as you mention - DISTINCT will help.
Try (COUNT(DISTINCT ?sent_email) AS ?sent_emails)

Related Links

Creating percentile_cont as aggregate function in Vertica
Create Expression in Report Builder 3.0 Report to sum a column
SAP Hana - STRING_AGG issue with ORDER BY CLAUSE
rrdtool does not keep max
Tableau Aggregate Percentage
use aggregates (min, max, avg) in CONSTRUCT query [duplicate]
R: transposing and aggregating columns
SQL Query Aggregate Error
Usage of aggregate functions in MarkLogic
Querying customDimensions in Google Bigquery
NHibernate - QueryOver criteria appearing in Where instead in Having clause, error
Howto aggregate on full data set in Spring Batch jobs?
Creating an aggregate function fails
Necessity of declaration of function in c and cpp
Calculate window average in tableau
Complexity asymptotic relation (theta, Big O, little o, Big Omega, little omega) between functions

Categories

HOME
spring-boot
max
joomla
excel-formula
postgresql
url
kendo-ui-angular2
cors
fortran
snappy
arguments
redhawksdr
symfony-sonata
snmp
grid
confluence-rest-api
travis-ci-cli
handlebars.js
clip-path
pip
apache-httpclient-4.x
camunda
google-closure-compiler
background-image
email-client
akavache
android-6.0-marshmallow
multiple-inheritance
typeorm
2checkout
tikz
mplab
directions
luhn
epub
odp.net
mkdir
client-server
contenteditable
definition
pexpect
google-chrome-storage
opos
stream-socket-client
standard-error
cc
petapoco
vertex-shader
blitz3d
alarmmanager
easy-thumbnails
optional-parameters
alfred
class-design
foreground
deck.js
objectify
patching
hotkeys
nsoperationqueue
avi
markerspiderfier
dynamics-nav-2013
github-for-mac
ios8-share-extension
callfire
web-standards
fileinputstream
data-representation
teamstudio-unplugged
caption
method-overloading
clearcanvas
parsefloat
windows-search
agent-based-modeling
android-relativelayout
html5-filesystem
mdp
mysql-error-1146
thucydides
nokogiri
biztalk-deployment
setcookie
urlfetch
nsregularexpression
correctness
github-archive
mysql-connector
stl-algorithm
scriptmanager
overlays
method-call
system-analysis
time-management
vista64

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App