I am trying to list the largest lake in each country in the world for a school exercise. I am using this dbpedia endpoint. Here is what i have:
select ?country (MAX(?area) AS ?maxarea)
where {
 ?lake rdfs:label ?label .
 ?lake rdf:type dbo:Lake .
 ?lake dbo:areaTotal ?area .
 ?lake dbo:country ?country .
 FILTER (lang(?label) = 'en') .
}
group by ?country
I get the country and the area of its largest lake which is the desired result but without the name of the lake. I just want to list the name of the lake in the table. So when I add ?label to the select, it complains that
Variable ?label is used in the result set outside aggregate and not mentioned in GROUP BY clause
If I add the ?label to GROUP BY, then it groups with ?country and ?label which it tries to get the max of. This results in listing all the possible lakes(when it groups by country and by lake, each group has one element, because country lake combination is always unique) which is not what I want.
I tried to solve this by using different kinds of joins and subqueries to no avail. Also SAMPLE doesn't work, because it just picks a random lake from the country instead of the largest lake. To make it clear, I am not asking for the answer but I am asking if there is a possible way to list a variable without adding to GROUP BY.
This answer doesn't really answer my original question which is if I can select a variable without using it in the GROUP BY. As far as I understand, I can list it with a subquery but I can't just have a variable without using it in the GROUP BY.
I solved the exercise question using a subquery like this:
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
    }
}
I added a ?country rdf:type dbo:country check to remove some regions like "Newark Basin".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With