sparql - 在参与者的维基数据查询中应用 group_concat

标签 sparql wikidata

我刚刚了解到 group_concatorder by , group by ,现在我正在尝试将它们应用于查询谋杀案。即,到下面的这个以获取所有参与者和目标。

SELECT DISTINCT ?incident ?label ?participant ?participantLabel ?target ?targetLabel
WHERE {
?incident wdt:P31 wd:Q132821.
?incident rdfs:label ?label.
optional{?incident wdt:P710 ?participant.}
optional{?incident wdt:P533 ?target. } }

我尝试申请 group_concatgroup by , order by . (在下面的 target 上没有做任何事情,因为即使这仅适用于参与者也不起作用):
SELECT DISTINCT ?incident ?label ?target ?targetLabel (group_concat(?participantLabel; separator=";") as ?participant)

WHERE {
?incident wdt:P31 wd:Q132821.
?incident rdfs:label ?label.
optional{?incident wdt:P710 ?participant.}
optional{?incident wdt:P533 ?target. }}
GROUP BY ?participant ?participantLabel
ORDER BY ?participantLabel

有人告诉我 Query is malformed: Bad aggregate .

是不是每个案例都有参与者?我该如何解决这个问题?

最佳答案

您需要从 wikidata 中阅读完整的错误消息。关键线在这里——

java.util.concurrent.ExecutionException: org.openrdf.query.MalformedQueryException: Bad aggregate
...
Caused by: org.openrdf.query.MalformedQueryException: Bad aggregate
...
Caused by: com.bigdata.rdf.sail.sparql.ast.VisitorException: Bad aggregate
...
Caused by: java.lang.IllegalArgumentException: Non-aggregate variable in select expression: incident

基本上,您 SELECT 中的所有非聚合变量也必须在您的 GROUP BY 中.通过我认为对您有益的其他一些调整,您的查询将变成这样——
SELECT DISTINCT ?incident 
                ?incidentLabel 
                ?target
                ?targetLabel
                ( GROUP_CONCAT ( DISTINCT ?participantLabel; separator="; " ) AS ?participants )
WHERE
  {
               ?incident     wdt:P31     wd:Q132821 .
               ?incident     rdfs:label  ?incidentLabel .
               FILTER ( LANGMATCHES ( LANG ( ?incidentLabel ), "en" ) ) 
    OPTIONAL { ?incident     wdt:P710    ?participant .
               ?participant  rdfs:label  ?participantLabel 
               FILTER ( LANGMATCHES ( LANG ( ?participantLabel ), "en" ) ) 
             }
    OPTIONAL { ?incident     wdt:P533    ?target . 
               ?target       rdfs:label  ?targetLabel 
               FILTER ( LANGMATCHES ( LANG ( ?targetLabel ), "en" ) ) 
             }
  }
GROUP BY ?incident ?incidentLabel ?target ?targetLabel
ORDER BY ?incidentLabel ?targetLabel 

我无法解释结果集中出现的重复行(向下滚动到“1991 Vic 轰炸”)。这些应该已经被 SELECT DISTINCT 中的一个或两个消除了。和 GROUP BY .

关于sparql - 在参与者的维基数据查询中应用 group_concat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55285976/

相关文章:

json - 如何将 Angular 中的单个 json 数据 "values"绑定(bind)到命名 $scope 变量(使用 SPARQL 查询返回的数据)?

java - 使用 sparql 查询向 Fuseki 插入数据

rdf - 通过SPARQL查询古腾堡项目catalog.rdf

java - 耶拿的 SPARQL 查询打印文字

sparql - 如何使用 SPARQL 使用实体名称查询 Wikidata 并检查替代标签?

java - 如何从 Jena RDF Inf 模型中的节点值获取个人名称

sparql - 如何根据外部标识符(例如 Spotify ID)搜索 wiki 数据?

wikidata - 维基数据更新期间出错

sparql - 如何查询使用 Wikidata 和 SPARQL 的人?

SPARQL:将多个谓词值组合成标签数组