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

Mit*_*ops 1 sparql wikidata

我正在寻找使用维基数据按人口重新创建德克萨斯州城市列表

我看到我可以用这个查询按人口做状态:

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)
Run Code Online (Sandbox Code Playgroud)

德克萨斯州的 id 是 wd:Q1439

所以我尝试了以下方法

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
Run Code Online (Sandbox Code Playgroud)

并且没有比赛。什么是正确的查询?

更新

将返回 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)
Run Code Online (Sandbox Code Playgroud)

Val*_*chi 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)
Run Code Online (Sandbox Code Playgroud)

诀窍是添加+next to wdt:P131,它将查询从“查找一条 wdt:P131边”转换为“查找一条或多条 wdt:P131边”。

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