Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GROUP BY in SPARQL?

Tags:

sparql

Assume that I uses the FOAF ontology. I want to return the name and the mbox for each person. I use the following query:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name ?email
WHERE {
  ?person foaf:name ?name.
  ?person foaf:mbox ?email.
}

The result of the join is a set of rows: ?person, ?name, ?email. This query is returning the ?name and ?email. Note that in some of the ?person may have multiple mailboxes, so in the returned set, a ?name row may appear multiple times, once for each mailbox.

Is there a solution to make a GROUP BY person ?name?

like image 663
khoubeil Avatar asked Dec 19 '25 01:12

khoubeil


1 Answers

You can group by person but then you need an aggregation for ?name and ?email

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT (sample(?name) AS ?name2) (sample(?email) as ?email2)
WHERE {
  ?person foaf:name ?name.
  ?person foaf:mbox ?email.
} GROUP BY ?person

SAMPLE picks one possible from the group for each ?person.

or maybe

SELECT (group_concat(?name) AS ?names)

(except that's a string).

It may be easier work with

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?email
WHERE {
  ?person foaf:name ?name.
  ?person foaf:mbox ?email.
} 
ORDER BY ?person ?name ?email

and process the results in your application where you know the incoming results have all the entries for one person is a single section of the results.

like image 178
AndyS Avatar answered Dec 21 '25 06:12

AndyS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!