Monday, 15 August 2011

sparql - How to merge RDF subjects with same properties summing their values? -


Given the following triple of:

  S 1 nameProperty "Bozo" S 1 laughProperty " Haha "S 1 valueProperty" 2.00 "^^ xml: Double s2 nameProperty" Joker "s2 laughProperty" hehe "valueProperty s2" 3.00 "^^ xml: Double S3 nameProperty" Bozo "S3 laughProperty" Haha "valueProperty S3" 1.00 "^^ Xml: double  

I want to merge topics with the same name and laugh and add their values, to some degree as a result:

  s1 nameProperty "Bozo" s1 laughProperty "Haha" valueProperty S 1 "3.00" ^^ xml: Double s2 nameProperty "Joker" s2 laughProperty "hehe" va LueProperty s2 "3.00" ^^ xml: double  

How to perform this with SPARQL with the highest efficiency? (There is no need to maintain the subjects. They can be inserted as long as nameProperty and laughProperty If you provide data, then it is usually useful if we actually provide data The questions can run upwards. Here the data corresponds to yours, but we can actually work with:

  @prefix XSD:  

This is a very straightforward build query, only the hard part is that when we need Groups , then we have nested < Strong> Select query so that we get the sum and sample total functions

  prefix: & lt; Urn: ex: & gt; Construction {? Clown: nameProperty? Name; Laughed laugh : ValueProperty? Total} where {{select (as sample (? S))? Name? Laughter (as the sum (? Value)? Total) Where {? S: nameProperty? Name; Laughed laugh ? ValueProperty value} group laughs by name}}  

Result (N3 and n three times just to be sure that 3.0e0 is actually a XSD: double):

< Pre> @prefix: & lt; Urn: ex: & gt; . @PrinxXSD: & lt; Http: //www.w3.org/2001/XMLSchema#> . : S3: Hamp Property "Haha"; : Name Property "Bozo"; : ValueProperty 3.0e0: S2: Laughing Property "Heh"; : Name Property "Joker"; : ValueProperty "3.00" ^^ xsd: Double


No comments:

Post a Comment