Is possible to concatenate equivalent results in a SPARQL query? For instance, consider the following RDF data and desired result:
@prefix gr:<http://purl.org/goodrelations/v1#>.
:prod_A gr:color "Red";
gr:color "Blue".
:prod_B gr:color "Green".
| ?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.
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.