query-e2d25199321bdceeecf713e1260b0856

rq turtle/ttl

Total number of days served by MPs

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 (sum(?duration) as ?total)
   WHERE {
     { ?mp p:P39 ?ps . ?ps ps:P39 ?term . ?ps pq:P580 ?start . ?ps pq:P582 ?end . 
       ?term wdt:P279 wd:Q16707842 . 
       BIND((?end-?start) AS ?duration) }
     # period for all ended terms
     union
     { ?mp p:P39 ?ps . ?ps ps:P39 ?term . ?ps pq:P580 ?start .
       ?term wdt:P279 wd:Q16707842 .
       filter not exists { ?ps pq:P582 ?end } . BIND(floor((NOW() - ?start)) as ?duration) }
     # time elapsed in a term which is still ongoing; floor rounds down to whole days
     SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} group by ?mp ?mpLabel 
# blank/zero values showing up because of start/end novalue, but we can work on those

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v7("?duration"):::projected v5("?end") v1("?mp"):::projected v2("?ps") v4("?start") v3("?term") v7("?total") c8(["bd:serviceParam"]):::iri c6(["wd:Q16707842"]):::iri c10(["en"]):::literal subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; f0[["not "]] subgraph f0e0["Exists Clause"] e0v1 --"p:qualifier/P582"--> e0v2 e0v2("?end"):::projected e0v1("?ps"):::projected end f0--EXISTS--> f0e0 f0 --> v2 f0 --> c4 f0 --> v5 v2 --"p:qualifier/P582"--> v5 v1 --"p:P39"--> v2 v2 --"p:statement/P39"--> v3 v2 --"p:qualifier/P580"--> v4 v3 --"p:direct/P279"--> c6 bind1[/"numeric-floor(NOW() - ?start)"/] v4 --o bind1 bind1 --as--o v7 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v1 --"p:P39"--> v2 v2 --"p:statement/P39"--> v3 v2 --"p:qualifier/P580"--> v4 v2 --"p:qualifier/P582"--> v5 v3 --"p:direct/P279"--> c6 bind2[/"?end - ?start"/] v5 --o bind2 v4 --o bind2 bind2 --as--o v7 end union0r <== or ==> union0l end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c8 --"wikibase:language"--> c10 end bind4[/"sum(?duration)"/] v7 --o bind4 bind4 --as--o v7