Sparql 在参与者的wikidata查询中应用group_concat

Sparql 在参与者的wikidata查询中应用group_concat,sparql,wikidata,Sparql,Wikidata,我刚刚学习了groupconcat和orderby,groupby,现在我正试图将它们应用于查询谋杀案件。也就是说,到下面这一步,获取所有参与者和目标 SELECT DISTINCT ?incident ?label ?participant ?participantLabel ?target ?targetLabel WHERE { ?incident wdt:P31 wd:Q132821. ?incident rdfs:label ?label. optional{?incident wdt

我刚刚学习了
groupconcat
orderby
groupby
,现在我正试图将它们应用于查询谋杀案件。也就是说,到下面这一步,获取所有参与者和目标

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. } }
我试着应用
分组concat
分组依据
订单依据
。(没有在下面的
目标上执行任何操作,因为即使只针对参与者也不起作用):

有人告诉我


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

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

基本上,
SELECT
中的所有非聚合变量也必须在
分组依据中。通过一些我认为对您有益的其他调整,您的查询变成了这样--


我无法解释结果集中出现的重复行(向下滚动到“1991”)。应通过
SELECT DISTINCT
groupby

两种方法中的一种或两种方法消除这些变量,您必须i)
groupby
选择的每个变量,或者ii)至少对其应用聚合函数。复制和粘贴不是学习SPARQL的方式,也不是学习计算机科学中任何其他东西的方式。我的意思是,
分组方式?参与者?参与者标签
-这对你有意义吗?你想让事件的所有参与者参与吗?那么,是什么让这群人来到这里的呢?那么
?参与者标签
应该从哪里来?你从我之前的回答中删除了标签服务,所以这不能很好地工作,我真希望我知道得更好,AKSW。我不是一个计算机科学的人,但上个月我第一次被介绍认识了所有这些。我想我每次使用它时都会变得更好,从这里的反馈来看,尽管我仍然很困惑(甚至不知道sparql中有“逐物分组”),所以我一直在尝试从互联网上看一些例子来学习,但我经常被卡住。Stackoverflow人是我唯一的老师,我很感激:)有很多SPARQL教程。这可能有点让人望而生畏,但值得一读,即使你不了解所有内容,所以你有一个想法,像
分组依据
顺序依据
限制
偏移量
,以及其他存在的东西。感谢链接,@TallTed,我尝试了一个教程,学习了真正的基础知识,然后很快它就需要一些背景知识:/我想我应该像你说的那样坚持下去,即使我不完全明白。
langmatches
函数有一定影响的罕见案例之一……target有3个英文标签:
en,en ca,en gb
-我检查了添加
(示例(lang(?targetLabel))为?langs)
到投影部分。因此,通过
部分从
分组中删除
?targetLabel
,并利用
?targetLabel
上的
示例
聚合函数,就可以完成这里的工作。谢谢,Talled!现在我明白了如何使用分组方式。@belle-太好了!请在这里(以及满足您之前问题的任何答案)。@Talled,我刚刚做到了!对不起,我不知道有这样一个选项。@TallTed格式不错;帮助说明SPARQL查询中的三元组
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
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 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