[英]How to transpose the query result in SPARQL
我正在使用 TopBraid Composer 编写 SPARQL 查询。 我查询了以下结果:
| Header | Total |
|-------- |------- |
| | |
| A | 5 |
| | |
| B | 6 |
| | |
| C | 7 |
| | |
| D | 8 |
现在我谦虚的问题是我们是否可以以某种方式转置结果如下:
| Header | A | B | C | D |
|-------- |--- |--- |--- |--- |
| Total | 5 | 6 | 7 | 8 |
是和否。 您使用的第一个符号对于理解 SPARQL SELECT 至关重要 - 每行代表数据上的一个单独的图形模式匹配,其中第一列显示 ?Header 的绑定,第二列显示 ?Total 的绑定,根据您未说明的查询。 例如,在其中一个匹配项中,?Header 绑定到“A”而 ?Total 绑定到“5”。 另一个匹配是 ?Header = "B" 和 ?Total = "6" 等(我建议在 SPARQL 上做一些功课)
从那以后,任何计算 SPARQL 查询的语言都有一些迭代结果集的方法,您可以将它们放在倒排表中,如图所示。
所以,不,SPARQL 不能这样做(查看 SPARQL 图形模式匹配),但是您使用的任何语言都应该能够迭代结果集以获得您正在寻找的内容。
您可以使用过滤的左外连接查询来构建您自己的转置表(又名数据透视表)。
PREFIX wd: <http://cocreate-cologne.wiki.opencura.com/entity/>
PREFIX wdt: <http://cocreate-cologne.wiki.opencura.com/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://cocreate-cologne.wiki.opencura.com/prop/>
PREFIX ps: <http://cocreate-cologne.wiki.opencura.com/prop/statement/>
PREFIX pq: <http://cocreate-cologne.wiki.opencura.com/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
select ?item ?itemLabel ?enthalten_in1Label ?enthalten_in2Label ?enthalten_in3Label {
SELECT ?item ?itemLabel ?enthalten_in1Label ?enthalten_in2Label ?enthalten_in3Label WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
?item p:P3 ?statement.
?statement ps:P3 wd:Q15.
?statement pq:P13 wd:Q17.
OPTIONAL { ?item wdt:P11 ?buendnis. FILTER (?buendnis in (wd:Q32)) }
OPTIONAL { ?item wdt:P11 ?sdgKarte. FILTER (?sdgKarte in (wd:Q14)) }
OPTIONAL { ?item wdt:P11 ?agora. FILTER (?agora in (wd:Q3)) }
BIND(?buendnis as ?enthalten_in1).
BIND(?sdgKarte as ?enthalten_in2).
BIND(?agora as ?enthalten_in3).
#debug
#Filter (?item in (wd:Q1))
}
LIMIT 2000
} ORDER BY ?itemLabel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.