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

Applying group_concat in wikidata queries for participants

我刚刚了解了group_concatorder bygroup 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 byorder 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

是不是每个case都有当事人?我该如何解决这个问题?

您需要从维基数据中阅读完整的错误信息。关键行在这里--

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 DISTINCTGROUP BY.

中的一个或两个消除了