如何在SPARQL中使用GroupBy和GroupConcat

问题描述 投票:0回答:1

我在对SPARQL查询进行分组时遇到麻烦。我正在使用http://kaiko.getalp.org/sparql编写查询。

我在一个字段上使用了GROUP BY。由于存在多个项目,因此我的结果是多个行,因此我试图将它们连接起来。

这是我的原始查询。

SELECT distinct ?word ?poslexinfo ?posdbnary ?def ?syn ?ant
WHERE { 
 ?f dbnary:describes ?lf. 
 ?lf rdf:type ontolex:LexicalEntry .
 ?lf lime:language "de" . 
 ?lf ontolex:canonicalForm ?wo .
 ?wo ontolex:writtenRep ?word .
 OPTIONAL {?lf lexinfo:partOfSpeech ?poslexinfo} .
 OPTIONAL {?lf dbnary:partOfSpeech ?posdbnary} .
 ?lf ontolex:sense ?os .
 ?os rdf:type ontolex:LexicalSense .
 OPTIONAL {?os dbnary:antonym ?ant} .
 OPTIONAL {?os dbnary:synonym ?syn} .
 ?os skos:definition ?defl .
 ?defl rdf:value ?def
 } LIMIT 5

结果:

word    poslexinfo  posdbnary   def     syn     ant

"gay"   http://www.lexinfo.net/ontology/2.0/lexinfo#adjective   "Adjektiv"   "sexuelle Neigungen zum eigenen Geschlecht zeigend" http://kaiko.getalp.org/dbnary/deu/homosexuell http://kaiko.getalp.org/dbnary/deu/hetero

"gay"   http://www.lexinfo.net/ontology/2.0/lexinfo#adjective   "Adjektiv"   "sexuelle Neigungen zum eigenen Geschlecht zeigend" http://kaiko.getalp.org/dbnary/deu/homosexuell http://kaiko.getalp.org/dbnary/deu/bi

这两行仅在反义词上有所不同,我的预期输出是,

word    poslexinfo  posdbnary   def     syn     ant

"gay"   http://www.lexinfo.net/ontology/2.0/lexinfo#adjective   "Adjektiv"   "sexuelle Neigungen zum eigenen Geschlecht zeigend" http://kaiko.getalp.org/dbnary/deu/homosexuell http://kaiko.getalp.org/dbnary/deu/hetero|http://kaiko.getalp.org/dbnary/deu/bi

我使用GroupBy和GroupConcat尝试了以下查询,不确定在这里我做错了什么。

查询:

SELECT distinct ?word ?poslexinfo ?posdbnary ?def ?syn (GROUP_CONCAT(?ant ; separator="|") AS ?ants)
WHERE { 
 ?f dbnary:describes ?lf. 
 ?lf rdf:type ontolex:LexicalEntry .
 ?lf lime:language "de" . 
 ?lf ontolex:canonicalForm ?wo .
 ?wo ontolex:writtenRep ?word .
 OPTIONAL {?lf lexinfo:partOfSpeech ?poslexinfo} .
 OPTIONAL {?lf dbnary:partOfSpeech ?posdbnary} .
 ?lf ontolex:sense ?os .
 ?os rdf:type ontolex:LexicalSense .
 OPTIONAL {?os dbnary:antonym ?ant} .
 OPTIONAL {?os dbnary:synonym ?syn} .
 ?os skos:definition ?defl .
 ?defl rdf:value ?def
 } GROUP BY ?word LIMIT 5

当我尝试上述查询时,出现此错误。

Virtuoso 37000错误SP031:SPARQL编译器:变量?syn用于结果集在聚合之外且未在GROUP BY子句中提及

sql group-by sparql group-concat
1个回答
0
投票

感谢@Jeen Broekstra!这里的建议有助于解决问题https://stackoverflow.com/a/34033146/73137

SELECT ?word ?poslexinfo ?def (GROUP_CONCAT(?syn ; separator="||") AS ?syns) (GROUP_CONCAT(?ant ; separator="||") AS ?ants)
                WHERE {
                    ?f dbnary:describes ?lf.
                    ?lf rdf:type ontolex:LexicalEntry .
                    ?lf lime:language "de" .
                    ?lf ontolex:canonicalForm ?wo .
                    ?wo ontolex:writtenRep ?word .
                    OPTIONAL {?lf lexinfo:partOfSpeech ?poslexinfo} .
                    OPTIONAL {?lf dbnary:partOfSpeech ?posdbnary} .
                    ?lf ontolex:sense ?os .
                    ?os rdf:type ontolex:LexicalSense .
                    OPTIONAL {?os dbnary:antonym ?ant} .
                    OPTIONAL {?os dbnary:synonym ?syn} .
                    ?os skos:definition ?defl .
                    ?defl rdf:value ?def
                } GROUP BY ?word ?syn ?def ?posdbnary ?poslexinfo LIMIT 20
© www.soinside.com 2019 - 2024. All rights reserved.