query-f3c90c7a0d5a67f8e85f0d54c2e76dc9

rq turtle/ttl

UK Parliament constituencies and the number of male & female MPs they have elected since 1997

SELECT DISTINCT ?constituency ?constituencyLabel ?created ?dissolved ?men_elected ?women_elected ?total { ?constituency wdt:P31 wd:Q27971968 . filter not exists { ?constituency wdt:P576 ?end . FILTER (?end <= "1997-05-01T00:00:00Z"^^xsd:dateTime) . } optional { ?constituency wdt:P576 ?end . bind(year(?end) as ?dissolved) . } optional { ?constituency wdt:P571 ?start . bind(year(?start) as ?created) . } # find all constituencies, omit any dissolved before 1997 optional { SELECT ?constituency (COUNT (distinct ?man) AS ?men_elected) WHERE { ?constituency wdt:P31 wd:Q27971968 . ?man p:P39 ?positionStatement . ?positionStatement pq:P768 ?constituency . ?positionStatement pq:P580 ?start . ?man wdt:P21 wd:Q6581097 . FILTER (?start >= "1997-05-01T00:00:00Z"^^xsd:dateTime) . } GROUP BY ?constituency }
# find and count all men elected (optional in case 0), omit any elected before 1997 optional { SELECT ?constituency (COUNT (distinct ?woman) AS ?women_elected) WHERE { ?constituency wdt:P31 wd:Q27971968 . ?woman p:P39 ?positionStatement . ?positionStatement pq:P768 ?constituency . ?positionStatement pq:P580 ?start . ?woman wdt:P21 wd:Q6581072 . FILTER (?start >= "1997-05-01T00:00:00Z"^^xsd:dateTime) . } GROUP BY ?constituency } # find and count all women elected (optional in case 0), omit any elected before 1997 { SELECT ?constituency (COUNT (distinct ?mp) AS ?total) WHERE { ?constituency wdt:P31 wd:Q27971968 . ?mp p:P39 ?positionStatement . ?positionStatement pq:P768 ?constituency . ?positionStatement pq:P580 ?start . FILTER (?start >= "1997-05-01T00:00:00Z"^^xsd:dateTime) . } GROUP BY ?constituency } # find and count all people elected, omit any elected before 1997 SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' } } ORDER BY ?men_elected ?women_elected

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 pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# UK Parliament constituencies and the number of male & female MPs they have elected since 1997

SELECT DISTINCT ?constituency ?constituencyLabel ?created ?dissolved ?men_elected ?women_elected ?total
{
 ?constituency wdt:P31 wd:Q27971968 . 
  filter not exists { ?constituency wdt:P576 ?end . FILTER (?end <= "1997-05-01T00:00:00Z"^^xsd:dateTime) . }
  optional { ?constituency wdt:P576 ?end . bind(year(?end) as ?dissolved) . }
  optional { ?constituency wdt:P571 ?start . bind(year(?start) as ?created) . }
 # find all constituencies, omit any dissolved before 1997
  optional { SELECT ?constituency (COUNT (distinct ?man) AS ?men_elected) WHERE {
      ?constituency wdt:P31 wd:Q27971968 . 
      ?man p:P39 ?positionStatement . ?positionStatement pq:P768 ?constituency . ?positionStatement pq:P580 ?start . 
      ?man wdt:P21 wd:Q6581097 . FILTER (?start >= "1997-05-01T00:00:00Z"^^xsd:dateTime) . 
    } GROUP BY ?constituency }  
  # find and count all men elected (optional in case 0), omit any elected before 1997
  optional { SELECT ?constituency (COUNT (distinct ?woman) AS ?women_elected) WHERE {
      ?constituency wdt:P31 wd:Q27971968 . 
      ?woman p:P39 ?positionStatement . ?positionStatement pq:P768 ?constituency . ?positionStatement pq:P580 ?start . 
      ?woman wdt:P21 wd:Q6581072 . FILTER (?start >= "1997-05-01T00:00:00Z"^^xsd:dateTime) . 
    } GROUP BY ?constituency }
  # find and count all women elected (optional in case 0), omit any elected before 1997
  { SELECT ?constituency (COUNT (distinct ?mp) AS ?total) WHERE {
      ?constituency wdt:P31 wd:Q27971968 . 
      ?mp p:P39 ?positionStatement . ?positionStatement pq:P768 ?constituency . ?positionStatement pq:P580 ?start . 
      FILTER (?start >= "1997-05-01T00:00:00Z"^^xsd:dateTime) . 
    } GROUP BY ?constituency }
  # find and count all people elected, omit any elected before 1997
 SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
}
ORDER BY ?men_elected ?women_elected

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v4("?constituency"):::projected v7("?created"):::projected v5("?dissolved"):::projected v3("?end") v8("?man") v10("?men_elected"):::projected v11("?mp") v9("?positionStatement") v6("?start") v12("?total"):::projected v10("?woman") v11("?women_elected"):::projected c10(["wd:Q6581097"]):::iri c4(["wd:Q27971968"]):::iri c15(["en"]):::literal c11(["wd:Q6581072"]):::iri c13(["bd:serviceParam"]):::iri f0[["not "]] subgraph f0e0["Exists Clause"] e0f0[["?end <= '1997-05-01T00:00:00Z^^xsd:dateTime'"]] e0f0 --> e0v1 e0v2 --"p:direct/P576"--> e0v1 e0v2("?constituency"):::projected e0v1("?end"):::projected end f0--EXISTS--> f0e0 f0 --> v3 f0 --> v4 f0 --> c2 f1[["?end <= '1997-05-01T00:00:00Z^^xsd:dateTime'"]] f1 --> v3 v4 --"p:direct/P576"--> v3 v4 --"p:direct/P31"--> c4 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v4 -."p:direct/P576".-> v3 bind2[/"year-from-dateTime(?end)"/] v3 --o bind2 bind2 --as--o v5 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v4 -."p:direct/P571".-> v6 bind3[/"year-from-dateTime(?start)"/] v6 --o bind3 bind3 --as--o v7 end subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; f4[["?start >= '1997-05-01T00:00:00Z^^xsd:dateTime'"]] f4 --> v6 v4 -."p:direct/P31".-> c4 v8 --"p:P39"--> v9 v9 --"p:qualifier/P768"--> v4 v9 --"p:qualifier/P580"--> v6 v8 --"p:direct/P21"--> c10 bind6[/"count(?man)"/] v8 --o bind6 bind6 --as--o v10 end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; f7[["?start >= '1997-05-01T00:00:00Z^^xsd:dateTime'"]] f7 --> v6 v4 -."p:direct/P31".-> c4 v10 --"p:P39"--> v9 v9 --"p:qualifier/P768"--> v4 v9 --"p:qualifier/P580"--> v6 v10 --"p:direct/P21"--> c11 bind9[/"count(?woman)"/] v10 --o bind9 bind9 --as--o v11 end f10[["?start >= '1997-05-01T00:00:00Z^^xsd:dateTime'"]] f10 --> v6 v4 --"p:direct/P31"--> c4 v11 --"p:P39"--> v9 v9 --"p:qualifier/P768"--> v4 v9 --"p:qualifier/P580"--> v6 bind12[/"count(?mp)"/] v11 --o bind12 bind12 --as--o v12 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c13 --"wikibase:language"--> c15 end