query-6b944b8717ca2a91fbcedc2c8c9703ad

rq turtle/ttl

Query to return a "simplified career" for a given MP (set in the VALUES clause, here Gladstone).

It shows a line for each continuing period in the same party & seat, changing if affiliation or

seat switch, or if there is a substantial break in service (eg here 1845-47), BUT it skips over

periods of dissolution.

It adds a short summary line with CONCAT and a count of total elapsed days (including some

periods of dissolution, so may not quite mesh with other days-elapsed counts).

It explicitly filters out the "returned for two seats" cases that are not taken up but note it

does not yet cope with ongoing (current) terms.

SELECT distinct ?mp ?mpLabel ?partyLabel ?seatLabel ?start (min(?end) as ?end2) (round(?end2 - ?start) as ?days) (concat(?seatLabel, " (", ?partyLabel, "), ", str(year(?start)), "-", str(year(?end2))) as ?string) where { VALUES ?mp { wd:Q160852 } # set MP here # find all seat-party-start pairs for each continuing period of office { SELECT distinct ?mp ?mpLabel ?partyLabel ?seatLabel ?start WHERE { ?mp p:P39 ?ps. ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842. ?ps pq:P768 ?seat . ?ps pq:P4100 ?party . ?ps pq:P580 ?start. # find all terms of office with seat and party, and their start date filter not exists { ?ps pq:P2715 ?elec . ?elec wdt:P31 wd:Q15283424 . # omit any terms which started at a general election ?mp p:P39 ?ps0 . ?ps0 ps:P39 ?term0 . ?term0 wdt:P156 ?term . ?ps0 pq:P4100 ?party . ?ps0 pq:P768 ?seat . ?ps0 pq:P1534 wd:Q741182 . } # and where the MP served for the same party & seat at dissolution in the previous term filter not exists { ?ps pq:P1534 wd:Q50393121 } # omit any double-return seats which were not taken up SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # labels inside the queries to catch any oddities where seat item changes, but name does not
} ORDER BY (?mp) ?start } # and all corresponding seat-party-end pairs { SELECT distinct ?mp ?mpLabel ?partyLabel ?seatLabel ?end WHERE { ?mp p:P39 ?ps. ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842. ?ps pq:P768 ?seat . ?ps pq:P4100 ?party . ?ps pq:P582 ?end. # find all terms of office with seat and party, and their end date filter not exists { ?ps pq:P1534 wd:Q741182 . # omit any terms which end at dissolution ?mp p:P39 ?ps2 . ?ps2 ps:P39 ?term2 . ?term2 wdt:P155 ?term . ?ps2 pq:P4100 ?party . ?ps2 pq:P768 ?seat . ?ps2 pq:P2715 ?elec . ?elec wdt:P31 wd:Q15283424 . } # and where the MP comes back for the same party & seat at the next general election filter not exists { ?ps pq:P1534 wd:Q50393121 } # omit any double-return seats which were not taken up SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # labels inside the queries to catch any oddities where seat item changes, but name does not
} ORDER BY (?mp) ?end } # now take our starts as the key, and match each to its appropriate end - the next one along # this is the smallest end date which is still larger than the start date # so filter by larger here, and smallest using min in the SELECT clause filter(?end > ?start) . # note > not >= } group by ?mp ?mpLabel ?partyLabel ?seatLabel ?start order by ?start

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#>
# Query to return a "simplified career" for a given MP (set in the VALUES clause, here Gladstone).
# It shows a line for each continuing period in the same party & seat, changing if affiliation or
# seat switch, or if there is a substantial break in service (eg here 1845-47), BUT it skips over
# periods of dissolution.
# It adds a short summary line with CONCAT and a count of total elapsed days (including some
# periods of dissolution, so may not quite mesh with other days-elapsed counts).
# It explicitly filters out the "returned for two seats" cases that are not taken up but note it
# does not yet cope with ongoing (current) terms.

SELECT distinct ?mp ?mpLabel ?partyLabel ?seatLabel ?start (min(?end) as ?end2) (round(?end2 - ?start) as ?days)
(concat(?seatLabel, " (", ?partyLabel, "), ", str(year(?start)), "-", str(year(?end2))) as ?string)
where {
  VALUES ?mp { wd:Q160852 } # set MP here
  # find all seat-party-start pairs for each continuing period of office
  { SELECT distinct ?mp ?mpLabel ?partyLabel ?seatLabel ?start
    WHERE {
      ?mp p:P39 ?ps. ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842. 
      ?ps pq:P768 ?seat . ?ps pq:P4100 ?party . ?ps pq:P580 ?start. 
      # find all terms of office with seat and party, and their start date
      filter not exists { ?ps pq:P2715 ?elec . ?elec wdt:P31 wd:Q15283424 . 
                          # omit any terms which started at a general election
                          ?mp p:P39 ?ps0 . ?ps0 ps:P39 ?term0 . ?term0 wdt:P156 ?term .
                          ?ps0 pq:P4100 ?party . ?ps0 pq:P768 ?seat . ?ps0 pq:P1534 wd:Q741182 . }
                          # and where the MP served for the same party & seat at dissolution in the previous term
      filter not exists { ?ps pq:P1534 wd:Q50393121 } # omit any double-return seats which were not taken up
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      # labels inside the queries to catch any oddities where seat item changes, but name does not  
  } ORDER BY (?mp) ?start }
  # and all corresponding seat-party-end pairs
  { SELECT distinct ?mp ?mpLabel ?partyLabel ?seatLabel ?end
    WHERE {
      ?mp p:P39 ?ps. ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842.
      ?ps pq:P768 ?seat . ?ps pq:P4100 ?party . ?ps pq:P582 ?end.
      # find all terms of office with seat and party, and their end date
      filter not exists { ?ps pq:P1534 wd:Q741182 . 
                          # omit any terms which end at dissolution
                          ?mp p:P39 ?ps2 . ?ps2 ps:P39 ?term2 . ?term2 wdt:P155 ?term . 
                          ?ps2 pq:P4100 ?party . ?ps2 pq:P768 ?seat . 
                          ?ps2 pq:P2715 ?elec . ?elec wdt:P31 wd:Q15283424 . }
                          # and where the MP comes back for the same party & seat at the next general election
      filter not exists { ?ps pq:P1534 wd:Q50393121 } # omit any double-return seats which were not taken up
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      # labels inside the queries to catch any oddities where seat item changes, but name does not  
  } ORDER BY (?mp) ?end }
  # now take our starts as the key, and match each to its appropriate end - the next one along
  # this is the *smallest* end date which is still *larger* than the start date
  # so filter by larger here, and smallest using min in the SELECT clause
  filter(?end > ?start) . # note > not >= 
} group by ?mp ?mpLabel ?partyLabel ?seatLabel ?start order by ?start

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v14("?days") v5("?elec") v2("?end"):::projected v13("?end2"):::projected v3("?mp"):::projected v9("?party") v4("?ps") v6("?ps0") v11("?ps2") v10("?seat") v1("?start"):::projected v15("?string") v8("?term") v7("?term0") v12("?term2") c2(["wd:Q50393121"]):::iri c11(["wd:Q741182"]):::iri c13(["wd:Q16707842"]):::iri c16(["bd:serviceParam"]):::iri c5(["wd:Q15283424"]):::iri c18(["#91;AUTO_LANGUAGE#93;,en"]):::literal f0[["?end > ?start"]] f0 --> v2 f0 --> v1 bind1[/VALUES ?mp/] bind1-->v3 bind10(["wd:Q160852"]) bind10 --> bind1 f2[["not "]] subgraph f2e0["Exists Clause"] e0v1 --"p:qualifier/P1534"--> e0c2 e0v1("?ps"):::projected e0c2(["wd:Q50393121"]):::iri end f2--EXISTS--> f2e0 f2 --> v4 f2 --> c1 f2 --> c2 v4 --"p:qualifier/P1534"--> c2 f3[["not "]] subgraph f3e1["Exists Clause"] e1v1 --"p:qualifier/P2715"--> e1v2 e1v2 --"p:direct/P31"--> e1c3 e1v3 --"p:P39"--> e1v4 e1v4 --"p:statement/P39"--> e1v5 e1v5 --"p:direct/P156"--> e1v6 e1v4 --"p:qualifier/P4100"--> e1v7 e1v4 --"p:qualifier/P768"--> e1v8 e1v4 --"p:qualifier/P1534"--> e1c10 e1v2("?elec"):::projected e1v3("?mp"):::projected e1v7("?party"):::projected e1v1("?ps"):::projected e1v4("?ps0"):::projected e1v8("?seat"):::projected e1v6("?term"):::projected e1v5("?term0"):::projected e1c10(["wd:Q741182"]):::iri e1c3(["wd:Q15283424"]):::iri end f3--EXISTS--> f3e1 f3 --> v4 f3 --> c3 f3 --> v5 f3 --> c4 f3 --> c5 f3 --> v3 f3 --> c6 f3 --> v6 f3 --> c7 f3 --> v7 f3 --> c8 f3 --> v8 f3 --> c9 f3 --> v9 f3 --> c10 f3 --> v10 f3 --> c1 f3 --> c11 v4 --"p:qualifier/P2715"--> v5 v5 --"p:direct/P31"--> c5 v3 --"p:P39"--> v6 v6 --"p:statement/P39"--> v7 v7 --"p:direct/P156"--> v8 v6 --"p:qualifier/P4100"--> v9 v6 --"p:qualifier/P768"--> v10 v6 --"p:qualifier/P1534"--> c11 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v8 v8 --"p:direct/P279"--> c13 v4 --"p:qualifier/P768"--> v10 v4 --"p:qualifier/P4100"--> v9 v4 --"p:qualifier/P580"--> v1 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c16 --"wikibase:language"--> c18 end f4[["not "]] subgraph f4e2["Exists Clause"] e2v1 --"p:qualifier/P1534"--> e2c2 e2v1("?ps"):::projected e2c2(["wd:Q50393121"]):::iri end f4--EXISTS--> f4e2 f4 --> v4 f4 --> c1 f4 --> c2 v4 --"p:qualifier/P1534"--> c2 f5[["not "]] subgraph f5e3["Exists Clause"] e3v1 --"p:qualifier/P1534"--> e3c2 e3v2 --"p:P39"--> e3v3 e3v3 --"p:statement/P39"--> e3v4 e3v4 --"p:direct/P155"--> e3v5 e3v3 --"p:qualifier/P4100"--> e3v6 e3v3 --"p:qualifier/P768"--> e3v7 e3v3 --"p:qualifier/P2715"--> e3v8 e3v8 --"p:direct/P31"--> e3c10 e3v8("?elec"):::projected e3v2("?mp"):::projected e3v6("?party"):::projected e3v1("?ps"):::projected e3v3("?ps2"):::projected e3v7("?seat"):::projected e3v5("?term"):::projected e3v4("?term2"):::projected e3c2(["wd:Q741182"]):::iri e3c10(["wd:Q15283424"]):::iri end f5--EXISTS--> f5e3 f5 --> v4 f5 --> c1 f5 --> c11 f5 --> v3 f5 --> c6 f5 --> v11 f5 --> c7 f5 --> v12 f5 --> c19 f5 --> v8 f5 --> c9 f5 --> v9 f5 --> c10 f5 --> v10 f5 --> c3 f5 --> v5 f5 --> c4 f5 --> c5 v4 --"p:qualifier/P1534"--> c11 v3 --"p:P39"--> v11 v11 --"p:statement/P39"--> v12 v12 --"p:direct/P155"--> v8 v11 --"p:qualifier/P4100"--> v9 v11 --"p:qualifier/P768"--> v10 v11 --"p:qualifier/P2715"--> v5 v5 --"p:direct/P31"--> c5 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v8 v8 --"p:direct/P279"--> c13 v4 --"p:qualifier/P768"--> v10 v4 --"p:qualifier/P4100"--> v9 v4 --"p:qualifier/P582"--> v2 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c16 --"wikibase:language"--> c18 end bind7[/"min(?end)"/] v2 --o bind7 bind7 --as--o v13 bind8[/"numeric-round(?end2 - ?start)"/] v13 --o bind8 v1 --o bind8 bind8 --as--o v14 bind9[/"concat(?seatLabel,' (',?partyLabel,'), ',str(year-from-dateTime(?start)),'-',str(year-from-dateTime(?end2)))"/] null --o bind9 null --o bind9 v1 --o bind9 v13 --o bind9 bind9 --as--o v15