query-5f5a760b04e23ebfe58191883c6a92e3

rq turtle/ttl

Query 27A - British Politicians/Building Queries .Wikidata:WikiProject_British_Politicians/Building_QueriesTaken from the Wikidata page .Wikidata:WikiProject British Politicians/Sample Queries and the associated tutorials, and the more elaborate set of examples at Wikidata:SPARQL query service/Wikidata Query HelpIt is intended to complement the more general introduction at This page will work through some simple examples to demonstrate the model used for UK MPs, and how to write queries to make best use of them, introducing a few specialised features that can be particularly useful. NOTE: Left out some initial, very simple examples.These are values attached to the main statement to improve or extend it in some way.. qualifiersThe main reason to use this slightly more complicated "position statement" syntax, rather than the simple wdt:, is to allow us to access the statement's ?person wdt:P31 wd:Q5 . ?person p:P39 ?ps . ?ps ps:P39 wd:Q77685926 . ?ps pq:P580 ?start . RESULT: This goes into the position statement ?ps to find the start time qualifier ?start (note the pq: prefix).We can select a range of these by using a more detailed query:The data model used for the UK is constructed on a "per-Parliament" basis, where each position refers to the specific Parliamentary term. . (Q77685926)member of the 58th Parliament of the United Kingdom You will note that we have been looking at the specific period ?person wdt:P31 wd:Q5 . ?person p:P39 ?ps . ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842 . - eg a member of the 55th Parliament, the 37th Parliament, etc.(Q16707842)Member of Parliament of (P279)subclass of RESULT: This finds all MPs who hold a position (?ps), where the value (?term) of that position is NOTE: Leaving out reams of increasingly complex queries to attain the final statement, as below: are still in Parliament today.andWe could link two sets of queries together to find MPs who were serving before 1997,

Use at

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?person ?personLabel (MIN(?start2) as ?first) ?seatLabel ?partyLabel ?start WHERE
 {
   ?person wdt:P31 wd:Q5 . ?person p:P39 ?ps . 
   ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842 .
   ?ps pq:P580 ?start . ?ps pq:P4100 ?party . ?ps pq:P768 ?seat . 
   FILTER NOT EXISTS { ?ps pq:P582 ?end } .

   ?person p:P39 ?ps2 . ?ps2 ps:P39 ?term2 . ?term2 wdt:P279 wd:Q16707842 .
   ?ps2 pq:P580 ?start2 .
   FILTER(?start2 < "1997-05-01T00:00:00Z"^^xsd:dateTime) .

   SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
 } group by ?person ?personLabel ?seatLabel ?partyLabel ?start

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v3("?end") v11("?first") v7("?party") v4("?person"):::projected v2("?ps") v9("?ps2") v8("?seat") v6("?start"):::projected v1("?start2"):::projected v5("?term") v10("?term2") c8(["wd:Q16707842"]):::iri c15(["en"]):::literal c13(["bd:serviceParam"]):::iri c4(["wd:Q5"]):::iri f0[["?start2 < '1997-05-01T00:00:00Z^^xsd:dateTime'"]] f0 --> v1 f1[["not "]] subgraph f1e0["Exists Clause"] e0v1 --"p:qualifier/P582"--> e0v2 e0v2("?end"):::projected e0v1("?ps"):::projected end f1--EXISTS--> f1e0 f1 --> v2 f1 --> c2 f1 --> v3 v2 --"p:qualifier/P582"--> v3 v4 --"p:direct/P31"--> c4 v4 --"p:P39"--> v2 v2 --"p:statement/P39"--> v5 v5 --"p:direct/P279"--> c8 v2 --"p:qualifier/P580"--> v6 v2 --"p:qualifier/P4100"--> v7 v2 --"p:qualifier/P768"--> v8 v4 --"p:P39"--> v9 v9 --"p:statement/P39"--> v10 v10 --"p:direct/P279"--> c8 v9 --"p:qualifier/P580"--> v1 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c13 --"wikibase:language"--> c15 end bind3[/"min(?start2)"/] v1 --o bind3 bind3 --as--o v11