User:TweetsFactsAndQueries/Queries/UK parliaments with count of Johns and count of women

Originally posted on Twitter.

#defaultView:LineChart
SELECT ?date ?count ?label WITH {
  # 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.
  }
} AS %MPsWithParliament WITH {
  # count of Johns in each parliament
  SELECT ?parliament (COUNT(DISTINCT ?mp) AS ?johns) WHERE {
    INCLUDE %MPsWithParliament.
    ?mp wdt:P735 wd:Q4925477.
  }
  GROUP BY ?parliament
} AS %johns WITH {
  # count of women in each parliament
  SELECT ?parliament (COUNT(DISTINCT ?mp) AS ?women) WHERE {
    INCLUDE %MPsWithParliament.
    ?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
} AS %women WHERE {
  # get ?johns and ?women
  INCLUDE %johns.
  INCLUDE %women.
  # 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
Try it!