1

On wikidata, I run the following query to get all universities.

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q3918.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

I get:

 item        itemLabel
 wd:Q12432  Michigan Technological University
 wd:Q13164  Moscow State University
 wd:Q13335  Montesquieu University – Bordeaux IV
 wd:Q13338  University of Bordeaux 1
 wd:Q13340  Bordeaux Segalen University
 wd:Q15142  University of Massachusetts Amherst
 wd:Q15143  University of Massachusetts Lowell
 wd:Q15144  University of Massachusetts Boston

However, I'd like all the properties of these universities appended to my results, with each property represented by a column (if a university does not have a property, then it will just be NULL in that column for it).

Is this possible, and how do I do this if yes?

forgodsakehold
  • 870
  • 10
  • 26
  • Not possible in SPARQL. You have to define the columns in the projection part of the query. Clearly, you could just run two queries, the first gets all properties of universities. Then you build the second query based on the result of the first one by a bunch of OPTIONAL patterns. There is no need to do anything in a single query. The client side has anything to solve this task easily – UninformedUser Mar 02 '19 at 08:05
  • @AKSW Able to write the two queries and present it as an answer? – forgodsakehold Mar 02 '19 at 09:23
  • I'd kindly forward you to the great [answer of StanislavKralin](https://stackoverflow.com/questions/49862258/how-to-get-all-properties-for-only-a-specific-category-in-wikidata) to get all properties used by entities of a given type – UninformedUser Mar 02 '19 at 10:38
  • Regarding the second query. You have to build this dynamically in your app/code/script. I mean, creating the query string. `SELECT ?item ?p1 ?p2 ... ?pn {?item wdt:P31 wd:Q3918. OPTIONAL {?item :p1 ?p1 } OPTIONAL {?item :p2 ?p2 } ... OPTIONAL {?item :pn ?pn }}` - note, this can be really really expensive and lead to a timeout if there are too many properties ... – UninformedUser Mar 02 '19 at 10:41

0 Answers0