query-9f97b20c07b7041ba53dc93114e6793e

rq turtle/ttl

# # generate all members serving on a specific date

# # with their constituency, party group (amalgamated) and experience in days

# # seems valid back to 1997 but perhaps unreliable before that

SELECT DISTINCT ?item ?itemLabel ?constituencyLabel ?groupLabel ?experience_in_days where {
{ select ?item (sum(round(?duration)) as ?experience_in_days) WHERE { { bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) . ?item p:P39 ?ps . ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842 . ?ps pq:P580 ?start . ?ps pq:P582 ?end . filter ( ?end < ?date ) . BIND((?end - ?start) AS ?duration) . } # period for all previous terms union { bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) . ?item p:P39 ?ps . ?ps ps:P39 ?term . ?ps pq:P580 ?start . ?ps pq:P582 ?end . ?term wdt:P279 wd:Q16707842 . filter ( ?start <= ?date ) .filter ( ?end >= ?date ) . BIND((?date - ?start) AS ?duration) } # period for the term containing this date with an end union { bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) . ?item p:P39 ?ps . ?ps ps:P39 ?term . ?ps pq:P580 ?start .
?term wdt:P279 wd:Q16707842 . filter ( ?start <= ?date ) .filter not exists { ?ps pq:P582 ?end } . BIND((?date - ?start) AS ?duration) }
# period for an ongoing term containing this date } group by ?item }
{ select ?item ?constituency ?group where { bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) . ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?term . ?term wdt:P279 wd:Q16707842 . ?positionStatement pq:P580 ?start . ?positionStatement pq:P768 ?constituency . OPTIONAL { ?positionStatement pq:P582 ?end . } filter (?start <= ?date ) . filter (COALESCE(?end, NOW()) > ?date ) . OPTIONAL { ?positionStatement pq:P4100 ?party . BIND(IF(?party = wd:Q6467393, wd:Q9630 , (IF(?party = wd:Q61751194 , wd:Q327591 , (IF(?party = wd:Q67153570 , wd:Q327591 , ?party))) ) ) AS ?group) . } # use qualifier dates; if no end date use now ; also merge independent groups & labour co-op # touch wood this should give numbers for TODAY, so if they have changed today it gives the newest grouping } } SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' } }

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#>
# # # generate all members serving on a specific date
# # # with their constituency, party group (amalgamated) and experience in days
# # # seems valid back to 1997 but perhaps unreliable before that

SELECT DISTINCT ?item ?itemLabel ?constituencyLabel ?groupLabel ?experience_in_days 
where 
{  
  { select ?item (sum(round(?duration)) as ?experience_in_days)
    WHERE {
     { bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) .
       ?item p:P39 ?ps . ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842 . 
       ?ps pq:P580 ?start . ?ps pq:P582 ?end . 
       filter ( ?end < ?date ) . 
       BIND((?end - ?start) AS ?duration) . }
     # period for all previous terms
     union
     { bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) .
       ?item p:P39 ?ps . ?ps ps:P39 ?term . ?ps pq:P580 ?start .  ?ps pq:P582 ?end .
       ?term wdt:P279 wd:Q16707842 . filter ( ?start <= ?date ) .filter ( ?end >= ?date ) .
       BIND((?date - ?start) AS ?duration) } 
     # period for the term containing this date with an end 
     union
     { bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) .
       ?item p:P39 ?ps . ?ps ps:P39 ?term . ?ps pq:P580 ?start .  
       ?term wdt:P279 wd:Q16707842 . filter ( ?start <= ?date ) .filter not exists { ?ps pq:P582 ?end } .
       BIND((?date - ?start) AS ?duration) }     
     # period for an ongoing term containing this date 
   } group by ?item
  }  
  { select ?item ?constituency ?group
    where {
      bind(("1997-05-07T00:00:00Z"^^xsd:dateTime) as ?date) .
      ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?term . ?term wdt:P279 wd:Q16707842 .
      ?positionStatement pq:P580 ?start . ?positionStatement pq:P768 ?constituency . 
      OPTIONAL { ?positionStatement pq:P582 ?end . } filter (?start <= ?date ) . filter (COALESCE(?end, NOW()) > ?date ) .
      OPTIONAL { ?positionStatement pq:P4100 ?party . 
        BIND(IF(?party = wd:Q6467393, wd:Q9630 ,
               (IF(?party = wd:Q61751194 , wd:Q327591 , 
                  (IF(?party = wd:Q67153570 , wd:Q327591 , ?party)))  )  )  AS ?group) . }
      # use qualifier dates; if no end date use now ; also merge independent groups & labour co-op
      # touch wood this should give numbers for TODAY, so if they have changed today it gives the newest grouping
   }
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v10("?constituency") v9("?date") v8("?duration") v1("?end") v8("?experience_in_days"):::projected v12("?group") v3("?item"):::projected v11("?party") v9("?positionStatement") v4("?ps") v6("?start") v5("?term") c10(["bd:serviceParam"]):::iri c4(["wd:Q16707842"]):::iri c12(["en"]):::literal subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; subgraph union1[" Union "] subgraph union1l[" "] style union1l 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 --> v4 f0 --> c6 f0 --> v1 v4 --"p:qualifier/P582"--> v1 f1[["?start <= ?date"]] f1 --> v6 f1 --> v9 bind2[/"'1997-05-07T00:00:00Z^^xsd:dateTime'"/] bind2 --as--o v9 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v5 v4 --"p:qualifier/P580"--> v6 v5 --"p:direct/P279"--> c4 bind3[/"?date - ?start"/] v9 --o bind3 v6 --o bind3 bind3 --as--o v8 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; f4[["?end >= ?date"]] f4 --> v1 f4 --> v9 f5[["?start <= ?date"]] f5 --> v6 f5 --> v9 bind6[/"'1997-05-07T00:00:00Z^^xsd:dateTime'"/] bind6 --as--o v9 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v5 v4 --"p:qualifier/P580"--> v6 v4 --"p:qualifier/P582"--> v1 v5 --"p:direct/P279"--> c4 bind7[/"?date - ?start"/] v9 --o bind7 v6 --o bind7 bind7 --as--o v8 end union1r <== or ==> union1l end end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; f8[["?end < ?date"]] f8 --> v1 f8 --> v9 bind9[/"'1997-05-07T00:00:00Z^^xsd:dateTime'"/] bind9 --as--o v9 v3 --"p:P39"--> v4 v4 --"p:statement/P39"--> v5 v5 --"p:direct/P279"--> c4 v4 --"p:qualifier/P580"--> v6 v4 --"p:qualifier/P582"--> v1 bind10[/"?end - ?start"/] v1 --o bind10 v6 --o bind10 bind10 --as--o v8 end union0r <== or ==> union0l end bind12[/"sum(numeric-round(?duration))"/] v8 --o bind12 bind12 --as--o v8 f13[["?endNOW() > ?date"]] f13 --> v1 f13 --> v9 f14[["?start <= ?date"]] f14 --> v6 f14 --> v9 bind15[/"'1997-05-07T00:00:00Z^^xsd:dateTime'"/] bind15 --as--o v9 v3 --"p:P39"--> v9 v9 --"p:statement/P39"--> v5 v5 --"p:direct/P279"--> c4 v9 --"p:qualifier/P580"--> v6 v9 --"p:qualifier/P768"--> v10 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v9 -."p:qualifier/P582".-> v1 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v9 -."p:qualifier/P4100".-> v11 bind16[/"if(?party = 'wd:Q6467393','wd:Q9630',if(?party = 'wd:Q61751194','wd:Q327591',if(?party = 'wd:Q67153570','wd:Q327591',?party)))"/] v11 --o bind16 bind16 --as--o v12 end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c10 --"wikibase:language"--> c12 end