首页 > 解决方案 > SPARQL wikidata group-by:拥有多个城市的河流

问题描述

我想问wikidata 哪些河流有多个大城市。下面的查询给了我一个列表,其中?citycount是一个从 1 到 13 的整数。

# rivers and cities
SELECT ?river ?riverLabel (COUNT(?city) AS ?citycount)
WHERE {
  ?river wdt:P31 wd:Q4022. # ... is a river
  ?city wdt:P31 wd:Q1549591. # ... is a big city
  ?city wdt:P206 ?river.  # ... located in or next to body of water
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]".}
}
GROUP BY ?river ?riverLabel
ORDER BY DESC(?citycount)
LIMIT 100

如何将结果限制在?citycount大于 3 的河流?

标签: sparqlaggregationwikidata

解决方案


将@UninformedUser 的评论变成答案:

# rivers and cities
SELECT ?river ?riverLabel (COUNT(?city) AS ?citycount)
WHERE {
  ?river wdt:P31 wd:Q4022. # ... is a river
  ?city wdt:P31 wd:Q1549591. # ... is a big city
  ?city wdt:P206 ?river.  # ... located in or next to body of water
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]".}
}
GROUP BY ?river ?riverLabel
HAVING (?citycount > 3)    # ← important line
ORDER BY DESC(?citycount)

见:https ://www.w3.org/TR/sparql11-query/#have


推荐阅读