query-1d4b270de25f33f27a558292b12a3df6
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
- https://query.wikidata.org/sparql
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