2

I have RDF like the following:

resource: r1 <dc:title>Mathematics</dc:title><dc:title>Chemistry</dc:title><dc:size>39</dc:size>
resource:r2 <dc:title>Biology</dc:title><dc:size>42</dc:size>

And I have this SPARQL query to extract the values:

PREFIX dc: <http://purl.org/dc/elements/1.1/> 
select distinct ?resource ?title ?size  where {
    ?resource dc:title ?title
    ?resource dc:size ?size
}

Results:

resource  title        size 
r1        Mathematics  39
r1        Chemistry    39
r2        Biology      42

But I want to have the following results:

resource  title                   size
r1        Mathematics, Chemistry  39
r2        Biology                 42

How can I solve this problem?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
user3450666
  • 45
  • 1
  • 1
  • 5
  • Duplicated (and answered) at http://answers.semanticweb.com/questions/27216/select-combine-multiple-attribute-values-sparql-rdf – Jeen Broekstra Mar 24 '14 at 04:36
  • possible duplicate of [Aggregating results from SPARQL query](http://stackoverflow.com/questions/18212697/aggregating-results-from-sparql-query). In this case, all you need is `select ?resource group_concat(?title;separator=',') { ... } group by ?resource ?size`. – Joshua Taylor Mar 24 '14 at 13:40

1 Answers1

3

To get ?resource combined group on it. This also allows you to use GROUP_CONCAT to get the titles truned into a string (the exact order will depend on evaluation).

PREFIX dc: <http://purl.org/dc/elements/1.1/> 
select ?resource ?size (GROUP_CONCAT(?title) AS ?titles) where {
    ?resource dc:title ?title
    ?resource dc:size ?size
} GROUP BY ?resource ?size

But you may be better of with sorting and doing the fine grained processing in application code:

ORDER BY ?resource ?size ?title

instead of GROUP BY.

AndyS
  • 16,345
  • 17
  • 21