简体   繁体   English

如何在 SPARQL 中转置查询结果

[英]How to transpose the query result in SPARQL

I am using TopBraid Composer for writing SPARQL queries.我正在使用 TopBraid Composer 编写 SPARQL 查询。 I have queried the following result:我查询了以下结果:

| Header    | Total     |
|--------   |-------    |
|           |           |
| A         | 5         |
|           |           |
| B         | 6         |
|           |           |
| C         | 7         |
|           |           |
| D         | 8         |

Now my humble question is whether we can transpose the result somehow as follows:现在我谦虚的问题是我们是否可以以某种方式转置结果如下:

| Header    | A     | B     | C     | D     |
|--------   |---    |---    |---    |---    |
| Total     | 5     | 6     | 7     | 8     |

Yes and no.是和否。 The first notation you use is essential for understanding SPARQL SELECT - each row represents a separate graph pattern match on the data where the first column shows the binding for ?Header and the second column shows the binding for ?Total, per your unstated query.您使用的第一个符号对于理解 SPARQL SELECT 至关重要 - 每行代表数据上的一个单独的图形模式匹配,其中第一列显示 ?Header 的绑定,第二列显示 ?Total 的绑定,根据您未说明的查询。 Eg in one of the matches, ?Header is bound to "A" and ?Total is bound to "5".例如,在其中一个匹配项中,?Header 绑定到“A”而 ?Total 绑定到“5”。 Another match is ?Header = "B" and ?Total = "6", etc. (I'd suggest doing some homework on SPARQL)另一个匹配是 ?Header = "B" 和 ?Total = "6" 等(我建议在 SPARQL 上做一些功课)

From that, any language computing the SPARQL query will have some means of iterating over the result set, and you can place them in an inverted table as you show.从那以后,任何计算 SPARQL 查询的语言都有一些迭代结果集的方法,您可以将它们放在倒排表中,如图所示。

So, no, SPARQL can't do that (look into SPARQL graph pattern matching), but whatever language you are using should be able to iterate over the result set to get what you are looking for.所以,不,SPARQL 不能这样做(查看 SPARQL 图形模式匹配),但是您使用的任何语言都应该能够迭代结果集以获得您正在寻找的内容。

you can use a filtered left outer join query to build your own transposed table (aka pivot table).您可以使用过滤的左外连接查询来构建您自己的转置表(又名数据透视表)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM