query-a1abee72d7eea8bdb323958fecf38072

rq turtle/ttl

Average ages (approx.) of MPs at general elections, by party

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#>
SELECT distinct ?election ?electiondate ?electionLabel ?members ?labour ?conservative 
((?electionyear - ?all_birth) as ?all_age)
((?electionyear - ?lab_birth) as ?lab_age)
((?electionyear - ?con_birth) as ?con_age) where
# nb some strange rounding here - find average year of birth, then subtract from year of election
# still need to find a more sophisticated way of doing this!
{
  { SELECT distinct ?election ?electiondate ?electionyear where 
    { ?election wdt:P31 wd:Q15283424 . ?election wdt:P585 ?electiondate . 
      bind(year(?electiondate) as ?electionyear) . filter (?electionyear >= 1970 ) }
  } # find all general elections and their date (for sorting)

  { SELECT ?election (count(distinct ?item) as ?members) (round(avg(year(?birth))) as ?all_birth) where 
    { ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . 
      ?membership wdt:P2715 ?election . ?positionStatement pq:P2715 ?election .
      optional { ?item wdt:P569 ?birth . }
    } group by ?election
  } # all MPs

  { SELECT ?election (count(distinct ?item) as ?labour) (round(avg(year(?birth))) as ?lab_birth) where 
    { ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . 
      ?membership wdt:P2715 ?election . ?positionStatement pq:P2715 ?election .
      { ?positionStatement pq:P4100 wd:Q9630 } union { ?positionStatement pq:P4100 wd:Q6467393 }
      optional { ?item wdt:P569 ?birth . }
    } group by ?election
  } # as above, count only labour

  { SELECT ?election (count(distinct ?item) as ?conservative) (round(avg(year(?birth))) as ?con_birth) where 
    { ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . 
      ?membership wdt:P2715 ?election . ?positionStatement pq:P2715 ?election .
      ?positionStatement pq:P4100 wd:Q9626 .
      optional { ?item wdt:P569 ?birth . }
    } group by ?election
  } # as above, count only conservative


  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; v8("?_anon_42b5416610844a34809f33a099ae9de162535") v11("?_anon_42b5416610844a34809f33a099ae9de162543") v14("?_anon_42b5416610844a34809f33a099ae9de162550") v17("?all_age") v10("?all_birth"):::projected v7("?birth") v19("?con_age") v16("?con_birth"):::projected v15("?conservative"):::projected v3("?election"):::projected v1("?electiondate"):::projected v4("?electionyear"):::projected v4("?item") v18("?lab_age") v13("?lab_birth"):::projected v12("?labour"):::projected v9("?members"):::projected v6("?membership") v5("?positionStatement") c17(["en"]):::literal c11(["wd:Q9630"]):::iri c15(["bd:serviceParam"]):::iri c12(["wd:Q6467393"]):::iri c13(["wd:Q9626"]):::iri c3(["wd:Q15283424"]):::iri f0[["?electionyear >= '1970^^xsd:integer'"]] f0 --> v4 v3 --"p:direct/P31"--> c3 v3 --"p:direct/P585"--> v1 bind1[/"year-from-dateTime(?electiondate)"/] v1 --o bind1 bind1 --as--o v4 v4 --"p:P39"--> v5 v5 --"p:statement/P39"--> v6 v6 --"p:direct/P2715"--> v3 v5 --"p:qualifier/P2715"--> v3 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v4 -."p:direct/P569".-> v7 end bind4[/"average(year-from-dateTime(?birth))"/] v7 --o bind4 bind4 --as--o v8 bind5[/"count(?item)"/] v4 --o bind5 bind5 --as--o v9 bind6[/"numeric-round()"/] null --o bind6 bind6 --as--o v10 v4 --"p:P39"--> v5 v5 --"p:statement/P39"--> v6 v6 --"p:direct/P2715"--> v3 v5 --"p:qualifier/P2715"--> v3 subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; v5 --"p:qualifier/P4100"--> c12 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v5 --"p:qualifier/P4100"--> c11 end union0r <== or ==> union0l end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v4 -."p:direct/P569".-> v7 end bind9[/"average(year-from-dateTime(?birth))"/] v7 --o bind9 bind9 --as--o v11 bind10[/"count(?item)"/] v4 --o bind10 bind10 --as--o v12 bind11[/"numeric-round()"/] null --o bind11 bind11 --as--o v13 v4 --"p:P39"--> v5 v5 --"p:statement/P39"--> v6 v6 --"p:direct/P2715"--> v3 v5 --"p:qualifier/P2715"--> v3 v5 --"p:qualifier/P4100"--> c13 subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v4 -."p:direct/P569".-> v7 end bind14[/"average(year-from-dateTime(?birth))"/] v7 --o bind14 bind14 --as--o v14 bind15[/"count(?item)"/] v4 --o bind15 bind15 --as--o v15 bind16[/"numeric-round()"/] null --o bind16 bind16 --as--o v16 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c15 --"wikibase:language"--> c17 end bind17[/"?electionyear - ?all_birth"/] v4 --o bind17 v10 --o bind17 bind17 --as--o v17 bind18[/"?electionyear - ?lab_birth"/] v4 --o bind18 v13 --o bind18 bind18 --as--o v18 bind19[/"?electionyear - ?con_birth"/] v4 --o bind19 v16 --o bind19 bind19 --as--o v19