sparql - SPARQL wikidata group-by:获取答案中的分组项目(具有多个城市的河流)
问题描述
以下查询返回至少有 3 个大城市的河流。
# rivers with at least 3 big 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)
如何在答案中包含实际城市(及其标签)?顺序应该保持不变,即前 13 个结果应该是位于长江的城市,接下来的 11 个结果应该是位于莱茵河的城市,等等。
解决方案
您应该尝试使用GROUP_CONCAT
,见下文。
# rivers and cities
SELECT ?river ?riverLabel (COUNT(?city) AS ?citycount)
(GROUP_CONCAT(CONCAT(STR(?city), "--> ", ?cityLabel); SEPARATOR="; ") AS ?cities)
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
?city rdfs:label ?cityLabel .
FILTER(LANG(?cityLabel) = 'en')
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]".}
}
GROUP BY ?river ?riverLabel
HAVING (?citycount >= 3)
ORDER BY DESC(?citycount)
推荐阅读
- android - 无法在 kotlin 中获取 firebase 当前用户
- powershell - 从批处理文件 (.cmd) 执行时,如何获取 PowerShell 控制台信息以显示在命令窗口中?
- algorithm - 描述“在局部和全局最优决策之间进行权衡”的算法的技术术语
- c++ - 谷歌测试打印到 std::set
- javascript - chart.js 上的自动图例翻译
- sql-server - 连接和不存在操作
- python - FileNotFoundError:[Errno 2] 文件 C:
- python - TypeError: create() 接受 1 个位置参数,但给出了 2 个;Django REST 框架
- graphql - 有没有办法在 Chrome DevTools 中调试 Apollo GraphQL 订阅?
- python - 如何将剩余的“%”模数添加到变量中