鉴于以下三元组:
s1 nameProperty "Bozo"
s1 laughProperty "Haha"
s1 valueProperty "2.00"^^xml:double
s2 nameProperty "Clown"
s2 laughProperty "hehe"
s2 valueProperty "3.00"^^xml:double
s3 nameProperty "Bozo"
s3 laughProperty "Haha"
s3 valueProperty "1.00"^^xml:double
我想合并具有相同名称的主题并笑和总结他们的价值,结果有点像:
s1 nameProperty "Bozo"
s1 laughProperty "Haha"
s1 valueProperty "3.00"^^xml:double
s2 nameProperty "Clown"
s2 laughProperty "hehe"
s2 valueProperty "3.00"^^xml:double
如何以最高效率使用SPARQL执行此操作? (不需要保留主题。只要具有合并值的新主题共享nameProperty
和laughProperty
,就可以插入主题。)
答案 0 :(得分:2)
如果您提供我们可以实际运行查询的数据,通常会有所帮助。这里的数据类似于您的数据,但我们实际上可以使用:
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix : <urn:ex:>
:s1 :nameProperty "Bozo".
:s1 :laughProperty "Haha".
:s1 :valueProperty "2.00"^^xsd:double.
:s2 :nameProperty "Clown".
:s2 :laughProperty "hehe".
:s2 :valueProperty "3.00"^^xsd:double.
:s3 :nameProperty "Bozo".
:s3 :laughProperty "Haha".
:s3 :valueProperty "1.00"^^xsd:double.
这是一个非常简单的构造查询。唯一棘手的部分是,由于我们需要分组,我们必须使用嵌套的选择查询,以便我们可以使用总和和< strong>示例聚合函数。
prefix : <urn:ex:>
construct {
?clown :nameProperty ?name ;
:laughProperty ?laugh ;
:valueProperty ?total
}
where {
{ select (sample(?s) as ?clown) ?name ?laugh (sum(?value) as ?total) where {
?s :nameProperty ?name ;
:laughProperty ?laugh ;
:valueProperty ?value
}
group by ?name ?laugh }
}
结果(在N3和N-Triples中,只是为了确保3.0e0实际上是xsd:double):
@prefix : <urn:ex:> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:s3 :laughProperty "Haha" ;
:nameProperty "Bozo" ;
:valueProperty 3.0e0 .
:s2 :laughProperty "hehe" ;
:nameProperty "Clown" ;
:valueProperty "3.00"^^xsd:double .
<urn:ex:s2> <urn:ex:laughProperty> "hehe" .
<urn:ex:s2> <urn:ex:nameProperty> "Clown" .
<urn:ex:s2> <urn:ex:valueProperty> "3.00"^^<http://www.w3.org/2001/XMLSchema#double> .
<urn:ex:s3> <urn:ex:laughProperty> "Haha" .
<urn:ex:s3> <urn:ex:nameProperty> "Bozo" .
<urn:ex:s3> <urn:ex:valueProperty> "3.0e0"^^<http://www.w3.org/2001/XMLSchema#double> .