W3C home > Mailing lists > Public > public-rdf-dawg@w3.org > October to December 2009

GROUP_CONCAT

From: Steve Harris <steve.harris@garlik.com>
Date: Tue, 20 Oct 2009 18:54:22 +0100
Message-Id: <7A9A6812-610E-4FB1-A5A4-6A29B12161B9@garlik.com>
To: "public-rdf-dawg@w3.org Group" <public-rdf-dawg@w3.org>
GROUP_CONCAT() is an aggregate function that takes an aggregated  
variable, and returns a string - I guess a plain literal in RDF terms.

What it does it build a sting from all the aggregate values joined  
with some separator.

NULLs are removed from the values.

Example

data:

<> :a "foo", "bar", "baz" .

query:

SELECT GROUP_CONCAT(?y, "|") AS ?cat
WHERE {
    ?x :a ?y .
} ORDER BY ?y GROUP BY ?x

results:

?cat = "bar|baz|foo"

This is very useful if for example you want to display all of  
someone's names in FOAF:

SELECT ?person, GROUP_CONCAT(?name, ", ") AS ?names
WHERE {
   ?person foaf:mbox_sha1sum  
"efd5dda12dd4011bb051b5f294ec05f2a8c87e21" .
   ?person foaf:name ?name .
} GROUP BY ?person

As Andy said on the call, if you want the output to be machine  
readable (e.g. to treat it as an array on the client side) you may  
need to escape the values passed to GROUP_CONCAT.

- Steve
Received on Tuesday, 20 October 2009 17:54:54 GMT

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