I have the following SPARQL query to get the list of countries with the smallest density of population per km and their presidents (leaders):
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?populationdensity ?leader
WHERE {
?country a dbpedia-owl:Country ;
rdfs:label ?country_name ;
prop:populationDensityKm ?populationdensity ;
dbpedia-owl:leader ?leader .
FILTER (?populationdensity < 10 && langMatches(lang(?country_name), "en")) .
}
GROUP BY ?populationdensity
ORDER BY ASC(?populationdensity)
limit 10
As you can see, I am grouping results by population density, yet I am getting results which include numerous population densities duplicates: SPARQL Query
Can someone tell me what am I doing wrong? I assume it has something to do with list of leaders, where for each country more than one is return. Is there a way to limit that to 1 leader per country somehow?
The first thing is that you should put all variables you use in the group by clause. Virtuoso currently is loose in its parsing of queries and allows things it should not. The second is you need to select just one leader, if you don't care which one then you should use SAMPLE. If you want all of them then use a group_concat variation.
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?populationdensity (sample(?leader) as ?ls)
WHERE {
?country a dbpedia-owl:Country ;
rdfs:label ?country_name ;
prop:populationDensityKm ?populationdensity ;
dbpedia-owl:leader ?leader .
FILTER (?populationdensity < 10 && langMatches(lang(?country_name), "en")) .
}
GROUP BY ?country_name ?populationdensity
ORDER BY ASC(?populationdensity)
limit 10
If you want the current leader you need to replace the line
dbpedia-owl:leader ?leader .
With this
dbpprop:leaderTitle/dbpprop:incumbent ?leader .