你會發現這個答案,它描述了SPARQL的group_concat
,有用:
在SPARQL,當你有一個結果集查詢解決方案,可以group
在一個或多個合併具有這些共同變量的解決方案。例如,考慮數據
@prefix : <http://example.org/people/>.
:person1 :hasChild :child1, :child2, :child3 .
:person2 :hasChild :child4, :child5 .
:person3 :hasChild :child6 .
如果您運行下面的查詢就可以了
prefix : <http://example.org/people/>
select ?person ?child where {
?person :hasChild ?child .
}
你得到的結果是這樣的:通過結果
$ arq --data data.n3 --query query.sparql
----------------------
| person | child |
======================
| :person3 | :child6 |
| :person2 | :child5 |
| :person2 | :child4 |
| :person1 | :child3 |
| :person1 | :child2 |
| :person1 | :child1 |
----------------------
迭代,你在有你問題會產生你當前得到的輸出類型。我們想要做的是真正得到這樣的結果:
$ arq --data data.n3 --query query.sparql
----------------------------------------
| person | child |
========================================
| :person3 | :child6 |
| :person2 | :child4, :child5 |
| :person1 | :child1, :child2, :child3 |
----------------------------------------
而這正是group_by
讓我們做。像這樣的查詢:
prefix : <http://example.org/people/>
select ?person (group_concat(?child;separator=' and ') as ?children) where {
?person :hasChild ?child .
}
group by ?person
產生(請注意,在結果的變量是?children
,不?child
,因爲我們使用group_concat(...) as ?children
創建新的變量?children
):
$ arq --data data.n3 --query query.sparql
---------------------------------------------------------------------------------------------------------------------------
| person | children |
===========================================================================================================================
| :person3 | "http://example.org/people/child6" |
| :person1 | "http://example.org/people/child3 and http://example.org/people/child2 and http://example.org/people/child1" |
| :person2 | "http://example.org/people/child5 and http://example.org/people/child4" |
---------------------------------------------------------------------------------------------------------------------------
如果您使用這樣的查詢並遍歷結果,按照你的要求打印它們,你會得到你想要的結果。如果你確實想從人和兒童身上取消領先的http://example.org/people/
,你需要更多的字符串處理。例如,使用STRAFTER去除http://example.org/people/
前綴,你可以使用這樣的查詢:
prefix : <http://example.org/people/>
select
(strafter(str(?personX),"http://example.org/people/") as ?person)
(group_concat(strafter(str(?child),"http://example.org/people/");separator=' and ') as ?children)
where {
?personX :hasChild ?child .
}
group by ?personX
得到類似的結果:
$ arq --data data.n3 --query query.sparql
----------------------------------------------
| person | children |
==============================================
| "person3" | "child6" |
| "person2" | "child5 and child4" |
| "person1" | "child3 and child2 and child1" |
----------------------------------------------
,當你做你的打印,會給你的結果像
person3 has children child6
person2 has children child5 and child4
person1 has children child3 and child2 and child1
這段代碼不能是你實際運行的代碼;查詢綁定變量'?children',但是您正在檢索名爲'?child'的變量的值。 –