1

Is possible to concatenate equivalent results in a SPARQL query? For instance, consider the following RDF data and desired result:

knowledge base:

@prefix gr:<http://purl.org/goodrelations/v1#>.

:prod_A gr:color "Red";
        gr:color "Blue".
:prod_B gr:color "Green".

Ideal Result:

| ?prod   | ?color    |
|---------|-----------|
| :prod_A | Red, Blue |
| :prod_B | Green     |

The only way through that I see is to use something like this:

PREFIX gr:<http://purl.org/goodrelations/v1#>

SELECT ?prod ?color
WHERE {
   ?prod gr:color ?color1.
   OPTIONAL {
       ?prod gr:color ?color2.
       FILTER ( ?color1 != ?color2 )
       bind(concat(?color1,",",?color2) as ?color)
   }

but it will work perfectly only if I have exactly 2 matches. Is there a more general and efficient way to do it?

It will be very useful to kill the nearly duplicate results that came from the 1:n relations.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
ffa
  • 747
  • 1
  • 6
  • 14

1 Answers1

1

Modified to include comments from below:

You can try:

PREFIX gr: <http://purl.org/goodrelations/v1#>

SELECT ?prod (GROUP_CONCAT(DISTINCT ?color; separator=',') as ?colors)
WHERE {
    ?prod gr:color ?color.
}
GROUP BY ?prod

example can be found here.

Jörn Hees
  • 3,338
  • 22
  • 44
  • can I ask you on what resources is based this sparql endpoint? – ffa May 26 '15 at 09:05
  • It's one of the Virtuoso servers hosted by OpenLink Software to showcase the capabilities of their Virtuoso server software. The endpoint currently seems to say it hosts 62,415,478,623 triples after 120 s timeout (`select count(*) where { [] [] [] }` (it probably hosts more but i didn't want to wait longer)) from several datasets of the LOD Cloud: http://lod-cloud.net/ and "sponged" graphs. – Jörn Hees May 26 '15 at 12:56
  • 2
    There's no need for the subquery here. You can just do `select ?prod (group_concat(distinct ?color; separator=",") as ?colors) { ?prod gr:color ?color } group by ?prod`. – Joshua Taylor May 26 '15 at 16:33
  • @ffa Do note that the query here isn't quite legal; you'll need to fix it to use it with anything except Virtuoso. 1) It needs parentheses around `group_concat(...) as ?colors`. (Virtuoso doesn't check this, but the spec requires them.) 2) An implicit group for an aggregate can only be used when there are no non-group variables in the projection. That is, you can't do `select ?prod (group_concat(...) as ?colors)` unless you've got a `group_by` at the outermost level. (Again, Virtuoso's non-conformance makes it very easy to introduce bugs that will be hidden until you try another endpoint.) – Joshua Taylor May 26 '15 at 16:39
  • @ffa You can see a shortened and legal version in [this paste](http://pastebin.com/aNrZsu0S). – Joshua Taylor May 26 '15 at 16:41
  • thank you very much @Joshua, indeed I was wandering why it doesn't work with Stardog – ffa May 26 '15 at 19:45
  • sorry, i seem to work with virtuoso too often ;) – Jörn Hees May 26 '15 at 23:55
  • updated the answer. i hope it's yet another weird coincidence that the lod.openlinksw.com endpoint started responding with errors just after i ran the new query against it... – Jörn Hees May 27 '15 at 00:13