【问题标题】:SPARQL wikidata group-by: rivers with multiple citiesSPARQL wikidata group-by:拥有多个城市的河流
【发布时间】:2021-12-07 17:36:29
【问题描述】:

我想问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 的河流?

【问题讨论】:

  • HAVING(?citycount > 3) - 查看规格w3.org/TR/sparql11-query/#having
  • 我已经尝试过了,但是把那个子句放在ORDER BY ... 之后的最后,这给了我一个Query is malformed-错误。现在它起作用了。谢谢!我猜你故意选择评论而不是常规答案?对于复制过去的能力,我自己回答了这个问题,但我当然可以删除它以支持您的常规回答。

标签: sparql aggregation wikidata


【解决方案1】:

将@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/#having

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多