SPARQL の について説明しているこの回答がgroup_concat
役立つ場合があります。
SPARQL では、クエリ ソリューションの結果セットがある場合、group
1 つ以上の変数に対して、これらの変数が共通しているソリューションをマージできます。たとえば、データを考えてみましょう
@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