sparql - how to group count items in SPARQL, accumulating low hit entries?
问题描述
How do I count grouped entries in SPARQL, merging entries whose quantity is less than a specific factor?
Consider for example the Nobel Prize data. I could get a count of all family names with a query like
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name (count(*) as ?count) WHERE {
?id foaf:familyName ?name
}
GROUP BY $name
ORDER BY DESC($count)
How do I modify the query so it only returns the family names occuring at least 3 times, accumulating the other names as other.
解决方案
只需将您的包装SELECT
成另一个。
询问
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name_ (SUM(?count) AS ?count_) {
{
SELECT ?name (COUNT(*) AS ?count) {
?id foaf:familyName ?name
} GROUP BY ?name
}
BIND (IF(?count > 2, ?name, "Other") AS ?name_)
} GROUP BY ?name_ ORDER BY DESC(IF(?name_ = "Other", -1 , ?count_))
结果
name_ count_
----------- ---------
Smith 5
Fischer 4
Wilson 4
Lee 3
Lewis 3
Müller 3
Other 878
推荐阅读
- android - 如何以及在何处声明用于保存数据的全局变量
- websocket - 通过 Websockets 连接到 Mosquitto Broker 时出错
- javascript - 从 postgraphile GraphQL 程序生成的查询/突变
- rust - 将嵌套结构字段路径作为宏参数传递
- php - 如何在extjs中注册表格后发送验证电子邮件
- c++ - 通过多线程读取和修改向量
- python - 在 Ubuntu VM 上创建 TUN 设备似乎不起作用
- python - 我导入 PyNaCl,但每当我将 python 升级到较新版本时,它都会被删除( Python )
- java - 格式化字符串以适应输出控制台中的盒子形状
- c++ - 相当于`
` 在 C++ 中