简体   繁体   中英

How to transpose the query result in SPARQL

I am using TopBraid Composer for writing SPARQL queries. 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. Eg in one of the matches, ?Header is bound to "A" and ?Total is bound to "5". Another match is ?Header = "B" and ?Total = "6", etc. (I'd suggest doing some homework on 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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