4

I am trying to return results for a set of persons (Edinburgh University alumni) who have held political office. I would like to return the title label of the office held, along with the start and end dates for each office, with many individuals holding multiple positions. I seem to be able to get one or the other or can get it to work if the person only held one position, but can't get the two to come together where multiple offices were held.

My current version of the query is below. This will give me the start and end dates, but rather than the label if the political office, such as Member of the [x] Parliament of the United Kingdom, ?officeLabel returns a value such as: statement/Q4668868-E3734C7D-40F0-4D4A-8208-E3D6B8C944CB

SELECT DISTINCT ?alumni ?fullName ?roleLabel ?officeLabel ?start ?end WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  ?alumni wdt:P69 wd:Q160302.
  ?alumni rdfs:label ?fullName.
  ?alumni wdt:P106 ?role.
  #Use Values to separate out politicians - Q82955
  VALUES (?role) {
    (wd:Q82955)
  }
  #Select only where position of office is stated but make dates optional
  ?alumni p:P39 ?office.
  OPTIONAL { ?office pq:P580 ?start. }
  OPTIONAL { ?office pq:P582 ?end. }
  FILTER(LANGMATCHES(LANG(?fullName), "en"))
  FILTER(NOT EXISTS { FILTER(LANGMATCHES(LANG(?fullName), "en-ca")) })
  FILTER(NOT EXISTS { FILTER(LANGMATCHES(LANG(?fullName), "en-gb")) })
}
ORDER BY ?fullName
LIMIT 10
rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
Al Mac
  • 49
  • 1

1 Answers1

5

Yeah, I still get tripped up on qualifiers and the Wikidata Data Model too.

enter image description here

Diagram by By Michael F. Schönitzer - Own work, based on File:Rdf mapping.svg, CC BY 4.0, https://commons.wikimedia.org/w/index.php?curid=63880194

After going the "p: route" from the "item", you need the "ps: route" to get back to the "simple value".

So, using this to slightly modify your query gives the results I think you want.

SELECT DISTINCT ?alumni ?fullName ?roleLabel ?officeLabel ?start ?end WHERE {
  ?alumni wdt:P69 wd:Q160302.
  ?alumni rdfs:label ?fullName.
  ?alumni wdt:P106 ?role.
  VALUES (?role) {
    (wd:Q82955)
  }
  ?alumni p:P39 ?officeStmnt.
  ?officeStmnt ps:P39 ?office.
  OPTIONAL { ?officeStmnt pq:P580 ?start. }
  OPTIONAL { ?officeStmnt pq:P582 ?end. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  FILTER(LANGMATCHES(LANG(?fullName), "en"))
  FILTER(NOT EXISTS { FILTER(LANGMATCHES(LANG(?fullName), "en-ca")) })
  FILTER(NOT EXISTS { FILTER(LANGMATCHES(LANG(?fullName), "en-gb")) })
}
ORDER BY ?fullName
LIMIT 10

Link to query on Wikidata

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • To avoid confusion with your variable names, the statement itself would be `?office` – UninformedUser Mar 04 '19 at 05:55
  • 1
    Yeah, no problem. I mean, the query was already correct before and the names of variables do not have any meaning by definition. Could also be `?var1, ?var2, ...` It's more a way to help in understanding the query resp. it's purpose sometimes using speaking names. Thank's for updating the query. – UninformedUser Mar 04 '19 at 07:18
  • Thank you, everyone, this is doing exactly what I want now. I hadn't written anything much beyond fairly simple queries in Wikidata up to now, so understanding how the property. qualifier and statement prefixes work is very useful indeed. – Al Mac Mar 04 '19 at 10:46