Michele Buzzoni
Michele Buzzoni

Reputation: 207

Sparql result duplicate individuals

I try to launch a SPARQL query but the output shows me duplicate individuals. It's strange because with Snap SPARQL query everything is fine but with SPARQL query there are duplicates:enter image description here

I can't use DISTINCT because the real query I want to launch is this:

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX re: <http://www.semanticweb.org/buzzo/ontologies/2017/renewable_energy#>
SELECT ?individual (SUM(?result) AS ?totalDim)
WHERE {

?individual rdf:type owl:NamedIndividual .
?individual rdf:type re:Fotovoltaico .
?pannello re:èPannelloDi ?individual .
?pannello re:haDimensione ?dim .
?dim re:lunghezza ?lun .
?dim re:larghezza ?larg .
BIND((?lun*?larg) AS ?result) .
}
GROUP BY ?individual

I explain: this query take all photovoltaic panel dimension (?larg * ?lun), make the product for each dimension and sum the result for each panel belonging to its plant. DISTINCT is run at the end of the process so this query sum a lot of duplicate dimension. There's a way to apply DISTINCT before SUM?

for example: i've 3 panel with dimension 20x20, so the product is 20*20 = 400. 3 panel * 400 = 1200 for result. But if i've duplicates, like 5 duplicates for each panel, the query result is: 3 panel * 5 duplicates = 15 * 400 = 6000 this is my problem.

I'm using Protege.

Upvotes: 0

Views: 1359

Answers (1)

UninformedUser
UninformedUser

Reputation: 8465

Similar to SQL, the solution modifier DISTINCT can be used, see the specs:

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX re: <http://www.semanticweb.org/buzzo/ontologies/2017/renewable_energy#>
SELECT  DISTINCT ?individual 
WHERE {

?individual rdf:type owl:NamedIndividual .
?individual rdf:type re:Fotovoltaico .

}
ORDER BY ?individual

Update due to totally different query

You can remove duplicates before doing an aggregate function by using again DISTINCT inside of the SUM:

PREFIX  re:   <http://www.semanticweb.org/buzzo/ontologies/2017/renewable_energy#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT  ?individual (SUM(DISTINCT ?result) AS ?totalDim)
WHERE
  { ?individual  rdf:type      owl:NamedIndividual ;
              rdf:type         re:Fotovoltaico .
    ?pannello  re:èPannelloDi  ?individual ;
              re:haDimensione  ?dim .
    ?dim      re:lunghezza     ?lun ;
              re:larghezza     ?larg
    BIND(( ?lun * ?larg ) AS ?result)
  }
GROUP BY ?individual

Upvotes: 3

Related Questions