query-222f11d7dfb15bc26602baf400e0c7c4

rq turtle/ttl

Number of veteran MPs and completely new MPs returned at each general election

Note that "veteran" includes any previous experience, which may not have been in the previous term

Thus an MP who leaves, spends some time out of Parliament, and returns, will count as "veteran" not "new"

SELECT distinct ?election ?electionLabel ?members ?veterans ?new where { { SELECT distinct ?election ?electiondate where { ?election wdt:P31 wd:Q15283424 . ?election wdt:P585 ?electiondate .
filter (?electiondate >= "1832-01-01T00:00:00Z"^^xsd:dateTime) } # throttle at 1832 for now } # find all general elections and their date (for sorting)

{ SELECT ?election (count(distinct ?item) as ?members) where { ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . ?membership wdt:P2715 ?election . ?positionStatement pq:P2715 ?election . } group by ?election } # number of people elected in this election # this code finds anyone who served the parliamentary term linked to this election # with a P39 value which started with this election

{ SELECT ?election (count(distinct ?item) as ?veterans) where { ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . ?membership wdt:P2715 ?election . ?positionStatement pq:P2715 ?election . # item holds a term which began with the election ?item p:P39 ?positionStatement2 . ?positionStatement2 ps:P39 ?membership2 . ?positionStatement2 pq:P580 ?start . filter ( ?start < ?electiondate ) . ?election wdt:P585 ?electiondate . ?membership2 wdt:P279 wd:Q16707842 . # item also holds another term which began before the election # and, critically, that office was an MP! } group by ?election } # people who served in any previous term

bind((?members-?veterans) as ?new)

SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' } } order by desc(?electiondate)

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#>
# Number of veteran MPs and completely new MPs returned at each general election
# Note that "veteran" includes any previous experience, which may not have been in the previous term
# Thus an MP who leaves, spends some time out of Parliament, and returns, will count as "veteran" not "new"

SELECT distinct ?election ?electionLabel ?members ?veterans ?new where
{
  { SELECT distinct ?election ?electiondate where 
    { ?election wdt:P31 wd:Q15283424 . ?election wdt:P585 ?electiondate .  
    filter (?electiondate >= "1832-01-01T00:00:00Z"^^xsd:dateTime) } # throttle at 1832 for now
  } # find all general elections and their date (for sorting)

  { SELECT ?election (count(distinct ?item) as ?members) where 
    { ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . 
      ?membership wdt:P2715 ?election . ?positionStatement pq:P2715 ?election .
    } group by ?election
  } # number of people elected in this election
    # this code finds anyone who served the parliamentary term linked to this election
    # with a P39 value which started with this election

  { SELECT ?election (count(distinct ?item) as ?veterans) where 
    { ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . 
      ?membership wdt:P2715 ?election . ?positionStatement pq:P2715 ?election . 
     # item holds a term which began with the election
      ?item p:P39 ?positionStatement2 . ?positionStatement2 ps:P39 ?membership2 . 
      ?positionStatement2 pq:P580 ?start . filter ( ?start < ?electiondate ) .
      ?election wdt:P585 ?electiondate . ?membership2 wdt:P279 wd:Q16707842 .
     # item also holds another term which began before the election
     # and, critically, that office was an MP!
    } group by ?election
  } # people who served in any previous term

  bind((?members-?veterans) as ?new)

  SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
} order by desc(?electiondate)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?election"):::projected v1("?electiondate") v3("?item") v6("?members"):::projected v5("?membership") v9("?membership2") v11("?new"):::projected v4("?positionStatement") v8("?positionStatement2") v7("?start") v10("?veterans"):::projected c11(["wd:Q16707842"]):::iri c15(["en"]):::literal c13(["bd:serviceParam"]):::iri c3(["wd:Q15283424"]):::iri f0[["?electiondate >= '1832-01-01T00:00:00Z^^xsd:dateTime'"]] f0 --> v1 v2 --"p:direct/P31"--> c3 v2 --"p:direct/P585"--> v1 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v5 v5 --"p:direct/P2715"--> v2 v4 --"p:qualifier/P2715"--> v2 bind2[/"count(?item)"/] v3 --o bind2 bind2 --as--o v6 f3[["?start < ?electiondate"]] f3 --> v7 f3 --> v1 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v5 v5 --"p:direct/P2715"--> v2 v4 --"p:qualifier/P2715"--> v2 v3 --"p:P39"--> v8 v8 --"p:statement/P39"--> v9 v8 --"p:qualifier/P580"--> v7 v2 --"p:direct/P585"--> v1 v9 --"p:direct/P279"--> c11 bind5[/"count(?item)"/] v3 --o bind5 bind5 --as--o v10 bind6[/"?members - ?veterans"/] v6 --o bind6 v10 --o bind6 bind6 --as--o v11 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c13 --"wikibase:language"--> c15 end