1

给定以下三元组:

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。)

4

1 回答 1

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.

这是一个非常简单的构造查询。唯一棘手的部分是,由于我们需要group by,我们必须使用嵌套选择查询,以便我们可以使用sumsample聚合函数。

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> .
于 2015-03-16T23:38:00.637 回答