W3C home > Mailing lists > Public > public-rdf-dawg@w3.org > July to September 2010

GROUP_CONCAT DISTINCT

From: Axel Polleres <axel.polleres@deri.org>
Date: Mon, 20 Sep 2010 00:17:49 +0100
Message-Id: <2FEE5694-6542-4D2F-82CF-EB8D697CFA1E@deri.org>
To: SPARQL Working Group <public-rdf-dawg@w3.org>
It seems DISTINCT might be quite important for GROUP_CONCAT ...
I was just playing with the following use case


Data:
@prefix ex: <http://example.org/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

ex:alice a foaf:Person; foaf:name "Alice Wonderland";
           foaf:nick "Alice", "The real Alice".

ex:bob a foaf:Person;
       foaf:name "Robert Doe", "Robert Charles Doe", "Robert C. Doe";
       foaf:nick "Bob","Bobby","RobC","BobDoe".

ex:charles a foaf:Person;
       foaf:name "Charles Charles";
       foaf:nick "Charlie" . 
=============

My query should do the following:
pick one sample name per person, plus a concatenated list of nicknames

query, first version:

=============

PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
SELECT ( SAMPLE(?N) as ?Name) 
       ( GROUP_CONCAT(?M; SEPARATOR = ",") AS ?Nicknames )
WHERE { ?P a foaf:Person ; 
           foaf:name ?N ;
           foaf:nick ?M . }
GROUP BY ?P  

=============

doesn't work, since the nicknames appear repeated... I suppose this is where I'd need DISTINCT, but I couldn't get that
following version running yet with any implementation: 

=============
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ( SAMPLE(?N) as ?Name)
       ( GROUP_CONCAT( DISTINCT ?M; SEPARATOR = ",") AS ?Nicknames )
WHERE { ?P a foaf:Person ;
           foaf:name ?N ;
           foaf:nick ?M . }
GROUP BY ?P

=============

My expected result for the latter query was something like:
--------------------------------------------------------------------------------------------
| Name               | Nicknames                                                           |
============================================================================================
| "Robert C. Doe"    | "BobDoe,RobC,Bobby,Bob" |
| "Alice Wonderland" | "The real Alice,Alice"                                              |
| "Charles Charles"  | "Charlie"                                                           |
--------------------------------------------------------------------------------------------

Does anybody have that implemented/running yet?

best,
Axel
Received on Sunday, 19 September 2010 23:18:23 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 16:15:43 GMT