如何在 SPARQL 中使用 GroupBy 和 GroupConcat
How to use GroupBy and GroupConcat in SPARQL
我在使用 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 Error SP031: SPARQL compiler: Variable ?syn is used in
the result set outside aggregate and not mentioned in GROUP BY clause
谢谢@Jeen Broekstra!这里的建议有助于解决问题 .
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
我在使用 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 Error SP031: SPARQL compiler: Variable ?syn is used in the result set outside aggregate and not mentioned in GROUP BY clause
谢谢@Jeen Broekstra!这里的建议有助于解决问题
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