query-09b97e82cca6b390de00e2cab32d6fa2

rq turtle/ttl

ratio of female characters per fictional universe

(assuming that any item with “from fictional universe” and “sex or gender” is a fictional character)

SELECT ?universe ?universeLabel ?percentString ?females ?total WHERE { # add ?percent if you want to sort in the table { SELECT ?universe (SUM(?female) AS ?females) (COUNT() AS ?total) WHERE { ?character wdt:P1080 ?universe; wdt:P21 ?gender. BIND(IF(?gender IN (wd:Q6581072, wd:Q1052281), 1, 0) AS ?female) } GROUP BY ?universe } BIND(?females/?total AS ?ratio) BIND(100?ratio AS ?percent) BIND(CONCAT(SUBSTR(STR(?percent), 1, 5), "%") AS ?percentString) SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } ORDER BY DESC(?total)

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# ratio of female characters per fictional universe
# (assuming that any item with “from fictional universe” and “sex or gender” is a fictional character)
SELECT ?universe ?universeLabel ?percentString ?females ?total WHERE { # add ?percent if you want to sort in the table
  {
    SELECT ?universe (SUM(?female) AS ?females) (COUNT(*) AS ?total) WHERE {
      ?character wdt:P1080 ?universe;
                 wdt:P21 ?gender.
      BIND(IF(?gender IN (wd:Q6581072, wd:Q1052281), 1, 0) AS ?female)
    }
    GROUP BY ?universe
  }
  BIND(?females/?total AS ?ratio)
  BIND(100*?ratio AS ?percent)
  BIND(CONCAT(SUBSTR(STR(?percent), 1, 5), "%") AS ?percentString)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?total)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?character") v5("?female") v6("?females"):::projected v4("?gender") v8("?percent") v9("?percentString"):::projected v7("?ratio") v7("?total"):::projected v3("?universe"):::projected c4(["bd:serviceParam"]):::iri c6(["#91;AUTO_LANGUAGE#93;,en"]):::literal v2 --"wdt:P1080"--> v3 v2 --"wdt:P21"--> v4 list0c2(["wd:Q1052281"]):::iri list0c1(["wd:Q6581072"]):::iri list0c1 --o bind0 list0c2 --o bind0 bind0[/"if( in ,'1^^xsd:integer','0^^xsd:integer')"/] v4 --o bind0 bind0 --as--o v5 bind3[/"sum(?female)"/] v5 --o bind3 bind3 --as--o v6 bind4[/"count(*)"/] bind4 --as--o v7 bind5[/"?females / ?total"/] v6 --o bind5 v7 --o bind5 bind5 --as--o v7 bind6[/"'100^^xsd:integer' * ?ratio"/] v7 --o bind6 bind6 --as--o v8 bind7[/"concat(substring(str(?percent),'1^^xsd:integer','5^^xsd:integer'),'%')"/] v8 --o bind7 bind7 --as--o v9 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c4 --"wikibase:language"--> c6 end