维基数据按人口对给定州的城市进行排序

时间:2021-05-05 14:20:49

标签: sparql wikidata

我希望使用维基数据按人口重新创建德克萨斯州的 this list 个城市。

我发现我可以使用 this query 来按人口划分州:

SELECT DISTINCT ?state ?stateLabel ?population
{
  ?state wdt:P31 wd:Q35657 ;
           wdt:P1082 ?population .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?state ?population ?stateLabel
ORDER BY DESC(?population)

德克萨斯州的 id 是 wd:Q1439

所以我试过the following

SELECT ?country ?countryLabel ?state ?stateLabel ?city ?cityLabel ?population
WHERE
    {
#    ?state wdt:P31 wd:Q35657 .    # Give me an american state
    ?state wdt:P31 wd:Q1439 .      # that state is is Texas
    ?city wdt:P31 wd:Q515 .        # it is an instance of city
    ?city wdt:P17 ?country.        # get the country (for double-checking)
    ?city wdt:P361 ?state.         # get the state it belongs to
    ?city wdt:P1082 ?population .  # get the population of the city
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
    }
  ORDER BY DESC(?population) limit 68

并且没有匹配项。什么是正确的查询?

更新

这个returns data,但不正确。它错过了休斯顿、圣安东尼奥等城市。

SELECT ?mun ?munLabel ?population WHERE {
  {
    SELECT distinct ?mun ?population WHERE {
     values ?habitation {
       wd:Q3957 
       wd:Q515 
       wd:Q15284
      } 
      ?mun (wdt:P31/(wdt:P279*)) ?habitation;
        wdt:P131 wd:Q1439;
#        wdt:P625 ?loc; 
        wdt:P1082 ?population .                                 
    }
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
  }
}
ORDER BY DESC(?population)

1 个答案:

答案 0 :(得分:5)

问题是休斯顿和圣安东尼奥的位置分别被列为哈里斯县和贝克萨县,而这些县位于德克萨斯州。如果您尝试此查询,它应该可以工作:

SELECT ?mun ?munLabel ?population WHERE {
  {
    SELECT distinct ?mun ?population WHERE {
     values ?habitation {
       wd:Q3957 
       wd:Q515 
       wd:Q15284
      } 
      ?mun (wdt:P31/(wdt:P279*)) ?habitation;
        wdt:P131+ wd:Q1439; #Add '+' here for transitivity
#        wdt:P625 ?loc; 
        wdt:P1082 ?population .                                 
    }
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
  }
}
ORDER BY DESC(?population)

诀窍是在 + 旁边添加 wdt:P131,这会将查询从“查找 恰好一条 wdt:P131 边”转换为“查找 < em>一个或多个 wdt:P131 条边”。

这解决了这个问题,因为哈里斯县和贝克萨县本身被列为位于德克萨斯州。