How do i fit that sparql calculation? - syntax-error

this is my actual problem:
?var0 is a group variable and ?var1 is not. But whenever I try to validate the syntax, there comes the following error message:
Non-group key variable in SELECT: ?var1 in expression ( sum(?var0) / ?var1 )
The complete Query:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX cz: <http://www.vs.cs.hs-rm.de/ontostor/SVC#Cluster>
PREFIX n: <http://www.vs.cs.hs-rm.de/ontostor/SVC#Node>
SELECT ( (SUM(?var0) / ?var1) AS ?result)
WHERE{
?chain0 rdf:type rdfs:Property .
?chain0 rdfs:domain <http://www.vs.cs.hs-rm.de/ontostor/SVC#Cluster> .
?chain0 rdfs:range <http://www.vs.cs.hs-rm.de/ontostor/SVC#Node> .
?this ?chain0 ?arg0 .
?arg0 n:node_realtime_cpu ?var0 .
?this cz:node_count ?var1 .
}
My question is how to correct that calculation to fit the SPARQL syntax?

The immediate problem is that ?var1 is not grouped on, so a fix would be to simply append
GROUP BY ?var1
at the end of your query.
However, whether that gives you the calculation you actually want is another matter.
It's not quite clear what you're trying to calculate, but it looks as if you're attempting to determine the average node_realtime_cpu for a cluster. If that is the case, you can probably do your calculation by just using SPARQL's AVG function instead:
SELECT ( AVG(?var0) AS ?result)
WHERE{
?chain0 rdf:type rdfs:Property .
?chain0 rdfs:domain <http://www.vs.cs.hs-rm.de/ontostor/SVC#Cluster> .
?chain0 rdfs:range <http://www.vs.cs.hs-rm.de/ontostor/SVC#Node> .
?this ?chain0 ?arg0 .
?arg0 n:node_realtime_cpu ?var0 .
}
GROUP BY ?this // grouping on the cluster identifier so we get an average _per cluster_
Yet another alternative would be to keep your query as-is, but group on two variables:
GROUP BY ?this ?var1
Which is best depends on what your data looks like and what, exactly, you're trying to calculate.

Related

Aggregate inside Subquery for SPARQL

Im a using Virtuoso and DBpedia as an endpoint.
My purpose is to retrieve all movies which have a greater amount of actor than the mean number of actors for all movies.
I thought the following query would work:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT
DISTINCT ?film
COUNT(?actor) AS ?numActors
WHERE{
?film rdf:type dbp:Film .
?film dbp:starring ?actor .
{
SELECT
AVG(?numActors) AS ?avgNumActors
WHERE{
SELECT
?Sfilm
COUNT(?Sactor) AS ?numActors
WHERE{
?Sfilm rdf:type dbp:Film .
?Sfilm dbp:starring ?Sactor
}
}
}
}
GROUP BY ?film
HAVING (COUNT(?actor) > ?avgNumActors)
LIMIT 20
but I receveice the following error
Variable ?avgNumActors is used in the result set outside aggregate and not mentioned in GROUP BY clause
What am I doing wrong?

Why does this SPARQL query give wrong results?

I tried doing some sparql requests on http://dbpedia.org/sparql.
My sparql-request is this:
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER ( ?Todestag > "2016-01-01"^^xsd:date ) .
} ORDER BY ?Todestag
The problem:
Somehow this FILTER doesn’t work. The SPARQL request gives me all people who died on every day since the start of time in DBpedia. However, I just want people who died after 2016. Can anyone spot the mistake in the query or the syntax?
Here is the correct solution to the query, note the casting applied to the FILTER CLAUSE ( i.e. xsd:date(?Todestag ) :
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER ( xsd:date(?Todestag) > "2016-01-01"^^xsd:date ) .
} ORDER BY ?Todestag
Live Query Solution Page (format HTML Table).
Query Definition Page
Okay, I figured it out myself. The problem is definitely the filter. I have now changed the filter and the desired result appears. The filter must be: FILTER (str(?Todestag) >= "2016") .
Completely, it would look like this:
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER (str(?Todestag) >= "2016") .
} ORDER BY ?Todestag

How can I translate SPARQL query into English

Could you please translate this query into English?
I am trying to write a naive implementation in code.
PREFIX om-owl: <http://knoesis.wright.edu/ssw/ont/sensor-observation.owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX weather: <http://knoesis.wright.edu/ssw/ont/weather.owl#>
SELECT DISTINCT ?sensor ?value ?uom
FROM NAMED STREAM <http://www.cwi.nl/SRBench/observations> [NOW - 1 HOURS]
WHERE {
?observation om-owl:procedure ?sensor ;
rdf:type/rdfs:subClassOf* weather:PrecipitationObservation ;
om-owl:result ?result .
?result ?p1 ?value .
OPTIONAL {
?result ?p2 ?uom .
}
}
Any help will be appreciated
As I understand it:
SELECT DISTINCT ?sensor ?value ?uom
Give me all the distinct sensors name, their value and the uom (I am not familiar with sensors) that correspond to the following conditions :
?observation om-owl:procedure ?sensor ;
First, give me the observations related by a procedure to a sensor.
rdf:type/rdfs:subClassOf* weather:PrecipitationObservation ;
From these observations, take all those are subclasses of Precipitations.
om-owl:result ?result .
And extract me their result.
?result ?p1 ?value .
Take all their value.
OPTIONAL { ?result ?p2 ?uom . }
And if it exist, all their uom (?).
So in the end, it seems to get all the value of rainfall aggregated by hour for each sensor.

My SPARQL query doesn't work at all

I am currently trying to run my query but I keep getting the error that in line 0 the parentheses are not balanced at '}'
I have checked my whole code multiple times, but I don't seem to get it fixed. I am currently using the dbpedia endpoint.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX yago: <http://dbpedia.org/class/yago/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?country ?government ?population
WHERE{ ?country dct:subject <http://dbpedia.org/resource>/Category:Countries_in_Europe> ;
rdfs:label ?country;
dbo:government ?government.
?government rdfs:label ?government.
?population rdfs:subClassOf* dbo:PopulatedPlace
rdf:type dbpedia-owl:Country;
rdfs:label ?country ;
prop:populationEstimate ?population .
FILTER (?population < 3000000) .
FILTER ( lang(?country) AND (lang(?(government = 'en')
}
Three rows in the graph should be shown, First with the country as a title, second with the governmenttypes of the countries as a title and the 3rd should be a row with the population descending from the total of 3000000.
Thanks alot in advance for helping me out!
You have multiple errors in this query.
Several things that pop out at me.
Thing 1 --
?government rdfs:label ?government.
You've got several similar ?subject ?predicate ?subject constructions.
Thing 2 --
?population rdfs:subClassOf* dbo:PopulatedPlace
rdf:type dbpedia-owl:Country;
I think you need a semicolon after dbo:PopulatedPlace
Thing 3 --
FILTER ( lang(?country) AND (lang(?(government = 'en')
That FILTER breaks syntax several ways. I think this will do what you intend --
FILTER ( lang(?country) = 'en') .
FILTER ( lang(?government) = 'en') .
Thing 4 --
<http://dbpedia.org/resource>/Category:Countries_in_Europe>
You've got an extra > in mid-string.
Thing 5 --
dbpedia-owl:Country
I think that should be dbo:Country
Thing 6 --
prop:populationEstimate
I think that should be dbp:populationEstimate
There are MANY more issues... I am not sure you're really trying.

Pairwise comparison with SPARQL

I'd like to compare a collection of objects pairwise for a given similarity metric. The metric will be defined explicitly such that some properties much match exactly and some others can only be so different to each other (i.e., comparing floats: no more than a 50% SMAPE between them).
How would I go about constructing such a query? The output would ideally be an Nx2 table, where each row contains two IRIs for the comparable objects. Duplicates (i.e., 1==2 is a match as well as 2==1) are admissible but if we can avoid them that would be great as well.
I would like to run this on all pairs with a single query. I would probably be able to figure out how to do it for a given object, but when querying across all objects simultaneously this problem becomes much more difficult.
Does anyone have insights into how to perform this?
The idea is this:
PREFIX ex: <http://example.org/ex#>
SELECT DISTINCT ?subject1 ?subject2
WHERE {
?subject1 ex:belongs ex:commonCategory .
?subject2 ex:belongs ex:commonCategory .
?subject1 ex:exactProperty ?e .
?subject2 ex:exactProperty ?e .
?subject1 ex:approxProperty ?a1 .
?subject2 ex:approxProperty ?a2 .
FILTER ( ?subject1 > ?subject2 ) .
FILTER ( (abs(?a1-?a2)/(abs(?a1)+abs(?a2))) < 0.5 )
}
E.g., on DBpedia:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX umbel-rc: <http://umbel.org/umbel/rc/>
SELECT DISTINCT ?subject1 ?subject2
WHERE {
?subject1 rdf:type umbel-rc:Actor .
?subject2 rdf:type umbel-rc:Actor .
?subject1 dbo:spouse ?spouse1 .
?subject2 dbo:spouse ?spouse2 .
?subject1 dbo:wikiPageID ?ID1 .
?subject2 dbo:wikiPageID ?ID2 .
FILTER ( ?subject1 > ?subject2 ) .
FILTER ( ?spouse1 = ?spouse2 ) .
FILTER ( abs(?ID1-?ID2)/xsd:float(?ID1+?ID2) < 0.05 )
}
Thus, probably, Zsa Zsa Gabor and Magda Gabor are the same person.
Both were spouses of George Sanders and their wikiPageID's are not very different from each other.
Some explanations:
The ?subject1 > ?subject2 clause removes "permutation duplicates";
On the usage of xsd:float see this question.