Select (combine) multiple attribute values - SPARQL / RDF

大兔子大兔子 提交于 2019-12-11 13:01:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22584124/select-combine-multiple-attribute-values-sparql-rdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!