query-9901b37e842982c77f7d06fe4a7b5f1f

rq turtle/ttl

. This query is just on the edge of timing out (it took 59s when I ran it just now, and that's with the elections throttled back to only "since 1990": (Q741182)dissolution of parliament :(P1534)end cause their term there ended with and. In other words, they were elected for Term N, but they also were in office during Term N-1, until Parliament was dissolved for the electionThis is a straightforward query and it takes just a few seconds. However, I'd also like to write one which finds people who were in office

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 ?election ?electionLabel ?members ?veterans ?held_at_dissolution where
{
  { SELECT distinct ?election ?electiondate where 
    { ?election wdt:P31 wd:Q15283424 . ?election wdt:P585 ?electiondate . 
    filter (?electiondate >= "1990-01-01T00:00:00Z"^^xsd:dateTime) } # throttle to post-1990
  } # 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 ) .
      ?membership2 wdt:P279 wd:Q16707842 . ?election wdt:P585 ?electiondate .
     # item also holds another term which began before the election
     # and, critically, was an MP!
    } group by ?election
  } # people who served in any previous term

  { SELECT ?election (count(distinct ?item) as ?held_at_dissolution) 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
      ?membership wdt:P155 ?membership2 .
     # find the term which preceded that one
      ?item p:P39 ?positionStatement2 . ?positionStatement2 ps:P39 ?membership2 . 
     # find all items which hold that term
      ?positionStatement2 pq:P1534 wd:Q741182
     # and made it to dissolution
    } group by ?election
  } # people who served up to dissolution of the previous term

  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") v11("?held_at_dissolution"):::projected v3("?item") v6("?members"):::projected v5("?membership") v9("?membership2") v4("?positionStatement") v8("?positionStatement2") v7("?start") v10("?veterans"):::projected c14(["wd:Q741182"]):::iri c11(["wd:Q16707842"]):::iri c18(["en"]):::literal c16(["bd:serviceParam"]):::iri c3(["wd:Q15283424"]):::iri f0[["?electiondate >= '1990-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 v9 --"p:direct/P279"--> c11 v2 --"p:direct/P585"--> v1 bind5[/"count(?item)"/] v3 --o bind5 bind5 --as--o v10 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v5 v5 --"p:direct/P2715"--> v2 v4 --"p:qualifier/P2715"--> v2 v5 --"p:direct/P155"--> v9 v3 --"p:P39"--> v8 v8 --"p:statement/P39"--> v9 v8 --"p:qualifier/P1534"--> c14 bind7[/"count(?item)"/] v3 --o bind7 bind7 --as--o v11 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c16 --"wikibase:language"--> c18 end