query-1d4b270de25f33f27a558292b12a3df6

rq turtle/ttl

Politician's careersIs it possible to report on the set of periods, and the start & end dates of the periods, in which a politician held a specific UK Parliament seat (pq:P768) for a specific party (pq:P4100) for a set of contiguous parliamentry terms? A set of contiguous parliamentry terms is defined as terms that are linked back-to-back by the dissolution of one parliament (pq:P1534 wd:Q741182) and the election at a general election of the next parliament (pq:P2715 ?elec . ?elec wdt:P31 wd:Q15283424). th Parliament of the United Kingdom" statements (ps:P39 ?term . ?term wdt:P279 wd:Q16707842). n and his "Member of the Q335791#P39 earlier today, in this example concentating specifically on Herbert Morrison Twitter which bubbled up on User:Andrew GrayThis is a query from . Can he be helped in SPARQL? (So for Morrison, the report should have 5 rows each having a start & end date, not 10 rows each with either a start or an end date.) pythonAndrew's best shot in SPARQL, below, is tantalisingly close, but no cigar. I regret to inform that he has reached for

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 ?mp ?mpLabel ?partyLabel ?seatLabel ?start ?end WHERE {
  VALUES ?mp { wd:Q335791 } # Herbert Morrison
                            # find all details for this MP's terms
  ?mp p:P39 ?ps. ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842. 
  ?ps pq:P768 ?seat . ?ps pq:P4100 ?party .  
                            # focus on term start date
                            # skip any where this term began at general and last one ended at dissol
  { ?ps pq:P580 ?start.
  filter not exists { 
                            # this term started with a general election
    ?ps pq:P2715 ?elec . ?elec wdt:P31 wd:Q15283424 . 
                            # but the MP is involved in an immediately previous term
    ?mp p:P39 ?ps0 . ?ps0 ps:P39 ?term0 . ?term0 wdt:P156 ?term .
                            # for same seat & party, which ended in a dissolution
    ?ps0 pq:P4100 ?party . ?ps0 pq:P768 ?seat . ?ps0 pq:P1534 wd:Q741182 . } }
  union
                            # focus on term end date
                            # skip any where this term ends at dissol and next begins at general
  { ?ps pq:P582 ?end.
  filter not exists { 
    ?ps pq:P1534 wd:Q741182 .
    ?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 . } }

  BIND(COALESCE(?start, ?end) AS ?order)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY (?mp) (?order) ?start

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v8("?elec") v13("?end"):::projected v4("?mp"):::projected v14("?order") v7("?party") v4("?ps") v9("?ps0") v11("?ps2") v6("?seat") v3("?start"):::projected v5("?term") v10("?term0") v12("?term2") c12(["wd:Q741182"]):::iri c4(["wd:Q16707842"]):::iri c17(["bd:serviceParam"]):::iri c9(["wd:Q15283424"]):::iri c19(["#91;AUTO_LANGUAGE#93;,en"]):::literal bind0[/VALUES ?mp/] bind0-->v4 bind00(["wd:Q335791"]) bind00 --> bind0 v4 --"p:P39"--> v4 v4 --"p:statement/P39"--> v5 v5 --"p:direct/P279"--> c4 v4 --"p:qualifier/P768"--> v6 v4 --"p:qualifier/P4100"--> v7 subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; f1[["not "]] subgraph f1e0["Exists Clause"] e0v1 --"p:qualifier/P1534"--> e0c2 e0v2 --"p:P39"--> e0v3 e0v3 --"p:statement/P39"--> e0v4 e0v4 --"p:direct/P155"--> e0v5 e0v3 --"p:qualifier/P4100"--> e0v6 e0v3 --"p:qualifier/P768"--> e0v7 e0v3 --"p:qualifier/P2715"--> e0v8 e0v8 --"p:direct/P31"--> e0c10 e0v8("?elec"):::projected e0v2("?mp"):::projected e0v6("?party"):::projected e0v1("?ps"):::projected e0v3("?ps2"):::projected e0v7("?seat"):::projected e0v5("?term"):::projected e0v4("?term2"):::projected e0c2(["wd:Q741182"]):::iri e0c10(["wd:Q15283424"]):::iri end f1--EXISTS--> f1e0 f1 --> v4 f1 --> c11 f1 --> c12 f1 --> v4 f1 --> c1 f1 --> v11 f1 --> c2 f1 --> v12 f1 --> c14 f1 --> v5 f1 --> c6 f1 --> v7 f1 --> c5 f1 --> v6 f1 --> c7 f1 --> v8 f1 --> c8 f1 --> c9 v4 --"p:qualifier/P1534"--> c12 v4 --"p:P39"--> v11 v11 --"p:statement/P39"--> v12 v12 --"p:direct/P155"--> v5 v11 --"p:qualifier/P4100"--> v7 v11 --"p:qualifier/P768"--> v6 v11 --"p:qualifier/P2715"--> v8 v8 --"p:direct/P31"--> c9 v4 --"p:qualifier/P582"--> v13 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; f2[["not "]] subgraph f2e1["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 f2--EXISTS--> f2e1 f2 --> v4 f2 --> c7 f2 --> v8 f2 --> c8 f2 --> c9 f2 --> v4 f2 --> c1 f2 --> v9 f2 --> c2 f2 --> v10 f2 --> c10 f2 --> v5 f2 --> c6 f2 --> v7 f2 --> c5 f2 --> v6 f2 --> c11 f2 --> c12 v4 --"p:qualifier/P2715"--> v8 v8 --"p:direct/P31"--> c9 v4 --"p:P39"--> v9 v9 --"p:statement/P39"--> v10 v10 --"p:direct/P156"--> v5 v9 --"p:qualifier/P4100"--> v7 v9 --"p:qualifier/P768"--> v6 v9 --"p:qualifier/P1534"--> c12 v4 --"p:qualifier/P580"--> v3 end union0r <== or ==> union0l end bind3[/"?start?end"/] v3 --o bind3 v13 --o bind3 bind3 --as--o v14 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c17 --"wikibase:language"--> c19 end