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

incomplete results with aggregate (R)
How to do an aggregated OR on a column in Amazon Redshift
'Your query does not include the specified expression as part of an aggregate function' error for calculated fields
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

Categories

HOME
botframework
salesforce
mysqli
abc
glsl
data-modeling
assign
uml-designer
swap
docker-compose
box-api
select2
suitecrm
desktop-application
virtualenv
comma
google-closure-compiler
jsfiddle
md2
django-rest-auth
git-branch
klipfolio
procedural-generation
windowserror
scrapy-spider
sha
symlink
jopendocument
multiple-inheritance
setup-project
javapns
sphinx4
pushpad
ocl
edirectory
schedule
oracle-nosql
circle
favorites
node-soap
ilog
intersection
ansible-inventory
ragel
azure-storage-files
device-driver
mockserver
gem-fury
office-interop
redbean
piranha-cms
niagara-ax
activeandroid
stanford-nlp-server
serialversionuid
vertex
visualizer
sharpssh
seyren
jtextarea
tsc
suitesparse
x12
avaudiorecorder
trendline
std
bioconductor
play-json
php-gd
edgar
uinavigationitem
openhab
dpkt
connection-timeout
static-files
information-hiding
clearcanvas
rails-migrations
sigma-grid-control
adabas
svg-android
randomaccessfile
ia-32
leap-year
gmail-imap
brew
cryptarithmetic-puzzle
openafs
object-tag
javah
s60
system-analysis
qprocess
pkcs#7
retrospectiva
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