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



我希望使用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)

得克萨斯州的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

没有火柴。对此的正确查询是什么?

更新

这会返回数据,但不正确。它怀念休斯顿、圣安东尼奥等城市。

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)

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

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后面加上+;寻找恰好一个CCD_ 4边";至";寻找一个或多个wdt:P131边";。

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

最新更新