sparql - How to merge RDF subjects with same properties summing their values? -
given following triples:
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
i'd merge subjects same name , laugh , sum values, result like:
s1 nameproperty "bozo" s1 laughproperty "haha" s1 valueproperty "3.00"^^xml:double s2 nameproperty "clown" s2 laughproperty "hehe" s2 valueproperty "3.00"^^xml:double
how perform sparql efficiency? (there no need retain subjects. can inserted long new 1 merged values shares same nameproperty
, laughproperty
.)
it's helpful if provide data can run queries over. here's data analogous yours, can work with:
@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.
this pretty straightforward construct query. tricky part since need group by have use nested select query can use sum , sample aggregate functions.
prefix : <urn:ex:> construct { ?clown :nameproperty ?name ; :laughproperty ?laugh ; :valueproperty ?total } { { select (sample(?s) ?clown) ?name ?laugh (sum(?value) ?total) { ?s :nameproperty ?name ; :laughproperty ?laugh ; :valueproperty ?value } group ?name ?laugh } }
results (in n3 , n-triples, sure 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> .
Comments
Post a Comment