SPARQL not grouping my results properly

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?

Answers


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 .

Need Your Help

Can't Add A View to EF Data Model

entity-framework visual-studio-2008-sp1

I have a view that I am trying to add to my ADO.NET Entity Data Model. Every time I try to Update From Database, and check the view, it refreshes everything else, but does not add the view. I get...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.