SPARQL group by only one variable

In semantic web, sparql is a powerful language which can among others query for specific knowledge from provided rdf databases. Recently I tried to find out the largest lake in each country in the world from dbpedia. I came up with a query which resulted in the country and the area of its largest lake but without the name of the lake. I wanted to list the name of the lake in the table. So when I added a new variable to the select clause without putting it in group by clause, it complained that "Variable ?label is used in the result set outside aggregate and not mentioned in GROUP BY clause".

Calais Tags:

1 answer

This one is the BEST answer!

Apparently sparql doesn't allow non aggregate variables in the select clause unless they are also in the group by, which affects the query's meaning. So I came up with another solution which includes an intersection(join):

select ?country ?area ?lake
where {
?lake rdfs:label ?label .
?lake rdf:type dbo:Lake .
?lake dbo:areaTotal ?area .
?lake dbo:country ?country .
?country rdf:type dbo:Country .
FILTER (lang(?label) = 'en') .
FILTER(?maxarea = ?area)
{
select ?country (MAX(?area) AS ?maxarea)
where {
?lake rdfs:label ?label .
?lake rdf:type dbo:Lake .
?lake dbo:areaTotal ?area .
?lake dbo:country ?country .
?country rdf:type dbo:Country .
FILTER (lang(?label) = 'en') .
}
group by ?country
}
}

Comments

That's a very interesting solution, thanks for sharing this! You could also shorten your inner query a bit as there seems to be no need to retrieve e.g. also label in that inner query, only ?maxarea is of particular interest to "pass" it up, if I see it right... :)

Volodymyr Lipenko - Thu, 11/23/2017 - 10:10 :::