query-d2fd9342912f3b030c1a728405b262bb

rq turtle/ttl

This query identifies MPs and the final ends to their careers

It looks for

- the last time someone left office as an MP

- what the cause of their leaving office was

SELECT DISTINCT (count (?item) as ?count) ?causeLabel { ?membership wdt:P279 wd:Q16707842 . # find all MP positions ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . # and people who held such a position OPTIONAL { ?positionStatement pq:P768 ?constituency . } # constituency if known OPTIONAL { ?positionStatement pq:P4100 ?party . } # party if known ?positionStatement pq:P580 ?start . # all members who have a start date (need to block out any without full dates) ?positionStatement pq:P582 ?end . # all members who have an end date ?positionStatement pq:P1534 ?cause . FILTER NOT EXISTS { ?membership2 wdt:P279 wd:Q16707842 . ?item p:P39 ?positionStatement2 . ?positionStatement2 ps:P39 ?membership2 . ?positionStatement2 pq:P580 ?start2 . optional { ?positionStatement2 pq:P582 ?end2 .} BIND(COALESCE(?end2, ?start2) AS ?check) . filter (?check > ?end) } # filter out any where they came back to office at a later date # the COALESCE form is weird but catches any that had two overlapping final terms starting on different dates # which does sometimes happen for last term "returned in two places" SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' } } GROUP BY ?causeLabel ORDER BY ?count

Use at

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#>
# This query identifies MPs and the final ends to their careers
# It looks for 
# - the last time someone left office as an MP
# - what the cause of their leaving office was

SELECT DISTINCT (count (?item) as ?count) ?causeLabel 
{
 ?membership wdt:P279 wd:Q16707842 .  # find all MP positions
 ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership .  # and people who held such a position
 OPTIONAL { ?positionStatement pq:P768 ?constituency . } # constituency if known
 OPTIONAL { ?positionStatement pq:P4100 ?party . } # party if known
 ?positionStatement pq:P580 ?start . # all members who have a start date (need to block out any without full dates)
 ?positionStatement pq:P582 ?end . # all members who have an end date 
 ?positionStatement pq:P1534 ?cause .
 FILTER NOT EXISTS {
 ?membership2 wdt:P279 wd:Q16707842 .
 ?item p:P39 ?positionStatement2 . 
 ?positionStatement2 ps:P39 ?membership2 . 
 ?positionStatement2 pq:P580 ?start2 .
 optional { ?positionStatement2 pq:P582 ?end2 .}
 BIND(COALESCE(?end2, ?start2) AS ?check) . filter (?check > ?end)
 } # filter out any where they came back to office at a later date
  # the COALESCE form is weird but catches any that had two overlapping final terms starting on different dates
  # which does sometimes happen for last term "returned in two places"
 SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
}
GROUP BY ?causeLabel
ORDER BY ?count

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v14("?cause") v9("?check") v11("?constituency") v15("?count") v3("?end") v8("?end2") v5("?item"):::projected v9("?membership") v4("?membership2") v12("?party") v10("?positionStatement") v6("?positionStatement2") v13("?start") v7("?start2") c2(["wd:Q16707842"]):::iri c13(["en"]):::literal c11(["bd:serviceParam"]):::iri f0[["not "]] subgraph f0e0["Exists Clause"] e0f0[["?check > ?end"]] e0f0 --> e0v8 e0f0 --> e0v2 e0v3 --"p:direct/P279"--> e0c2 e0v4 --"p:P39"--> e0v5 e0v5 --"p:statement/P39"--> e0v3 e0v5 --"p:qualifier/P580"--> e0v6 subgraph optionale00["(optional)"] style optionale00 fill:#bbf,stroke-dasharray: 5 5; e0v5 -."p:qualifier/P582".-> e0v7 end e0bind1[/"?end2?start2"/] e0v7 --o e0bind1 e0v6 --o e0bind1 e0bind1 --as--o e0v8 e0v8("?check"):::projected e0v2("?end"):::projected e0v7("?end2"):::projected e0v4("?item"):::projected e0v3("?membership2"):::projected e0v5("?positionStatement2"):::projected e0v6("?start2"):::projected e0c2(["wd:Q16707842"]):::iri end f0--EXISTS--> f0e0 f0 --> v9 f0 --> v3 f0 --> v4 f0 --> c1 f0 --> c2 f0 --> v5 f0 --> c3 f0 --> v6 f0 --> c4 f0 --> c5 f0 --> v7 f0 --> c6 f0 --> v8 f1[["?check > ?end"]] f1 --> v9 f1 --> v3 v4 --"p:direct/P279"--> c2 v5 --"p:P39"--> v6 v6 --"p:statement/P39"--> v4 v6 --"p:qualifier/P580"--> v7 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v6 -."p:qualifier/P582".-> v8 end bind2[/"?end2?start2"/] v8 --o bind2 v7 --o bind2 bind2 --as--o v9 v9 --"p:direct/P279"--> c2 v5 --"p:P39"--> v10 v10 --"p:statement/P39"--> v9 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v10 -."p:qualifier/P768".-> v11 end subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v10 -."p:qualifier/P4100".-> v12 end v10 --"p:qualifier/P580"--> v13 v10 --"p:qualifier/P582"--> v3 v10 --"p:qualifier/P1534"--> v14 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c11 --"wikibase:language"--> c13 end bind4[/"count(?item)"/] v5 --o bind4 bind4 --as--o v15