我刚刚了解了group_concat
和order by
,group by
,现在我正试图将它们应用于查询谋杀案。 I.E.,请转到下面的这个列表,以获取所有参与者和目标。
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_concat
和group 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。
是因为不是每个案例都有参与者吗?我该如何解决?
答案 0 :(得分:1)
您需要从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维克爆炸”)。这些应该已经被SELECT DISTINCT
和GROUP BY
中的一个或两个都消除了。