query-c076d1fffeb609b887e928640f1a8d05

rq turtle/ttl

.on TwitterOriginally posted

Use at

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/>
#defaultView:LineChart
SELECT ?date ?count ?label WHERE {
  # get ?johns and ?women
   {
  # count of Johns in each parliament
  SELECT ?parliament (COUNT(DISTINCT ?mp) AS ?johns) WHERE {
     {
  # all MPs with their associated parliament
  SELECT DISTINCT ?mp ?parliament WHERE {
    ?mp wdt:P31 wd:Q5;
        p:P39/ps:P39 ?position.
    ?position wdt:P279 wd:Q16707842;
              p:P279/pq:P2937|wdt:P2937 ?parliament.
  }
}
    ?mp wdt:P735 wd:Q4925477.
  }
  GROUP BY ?parliament
}
   {
  # count of women in each parliament
  SELECT ?parliament (COUNT(DISTINCT ?mp) AS ?women) WHERE {
     {
  # all MPs with their associated parliament
  SELECT DISTINCT ?mp ?parliament WHERE {
    ?mp wdt:P31 wd:Q5;
        p:P39/ps:P39 ?position.
    ?position wdt:P279 wd:Q16707842;
              p:P279/pq:P2937|wdt:P2937 ?parliament.
  }
}
    ?mp wdt:P21/wdt:P279? wd:Q6581072. # (P279?: include trans women – none yet, but will probably happen in the future; the data model might change before then, though)
  }
  GROUP BY ?parliament
}
  # fan out single result
  # ?parliament ?johns ?women
  # into two results
  # ?parliament "Johns" ?johns
  # ?parliament "women" ?women
  # so the line chart works
  VALUES ?label { "Johns"@en "women"@en }
  BIND(IF(?label = "Johns"@en, ?johns, ?women) AS ?count)
  # get parliament date for the chart
  ?parliament wdt:P571|wdt:P580 ?date.
}
ORDER BY ?date

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v8("?count"):::projected v1("?date"):::projected v5("?johns") v7("?label"):::projected v2("?mp") v4("?parliament") v3("?position") v6("?women") a1((" ")) a2((" ")) a3((" ")) a4((" ")) a5((" ")) c6(["wd:Q16707842"]):::iri c13(["wd:Q6581072"]):::iri c11(["wd:Q4925477"]):::iri c2(["wd:Q5"]):::iri v2 --"p:direct/P31"--> c2 v2 --"p:P39"--> a1 a1 --"p:statement/P39"--> v3 v3 --"p:direct/P279"--> c6 subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; v3 --"p:direct/P2937"--> v4 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v3 --"p:P279"--> a2 a2 --"p:qualifier/P2937"--> v4 end union0r <== or ==> union0l end v2 --"p:direct/P735"--> c11 bind1[/"count(?mp)"/] v2 --o bind1 bind1 --as--o v5 v2 --"p:direct/P31"--> c2 v2 --"p:P39"--> a3 a3 --"p:statement/P39"--> v3 v3 --"p:direct/P279"--> c6 subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; v3 --"p:direct/P2937"--> v4 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; v3 --"p:P279"--> a4 a4 --"p:qualifier/P2937"--> v4 end union1r <== or ==> union1l end v2 --"p:direct/P21"--> a5 subgraph union2[" Union "] subgraph union2l[" "] style union2l fill:#abf,stroke-dasharray: 3 3; a5 --"p:direct/P279"--> c13 end subgraph union2r[" "] style union2r fill:#abf,stroke-dasharray: 3 3; end union2r <== or ==> union2l end bind3[/"count(?mp)"/] v2 --o bind3 bind3 --as--o v6 bind4[/VALUES ?label/] bind4-->v7 bind40([sJohns^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>"]) bind40 --> bind4 bind41([swomen^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>"]) bind41 --> bind4 bind5[/"if(?label = sJohns^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>',?johns,?women)"/] v7 --o bind5 v5 --o bind5 v6 --o bind5 bind5 --as--o v8 subgraph union3[" Union "] subgraph union3l[" "] style union3l fill:#abf,stroke-dasharray: 3 3; v4 --"p:direct/P580"--> v1 end subgraph union3r[" "] style union3r fill:#abf,stroke-dasharray: 3 3; v4 --"p:direct/P571"--> v1 end union3r <== or ==> union3l end