query-400f5c71d7476fa327ad92f60e66de62

rq turtle/ttl

Largest worldwide cities per country - final tweaks on an aggregate query on the OpenData StackExchange forum. original discussionI have a half-finished query that I would like to tweak. You can see the This query returns the maximum (not necessarily latest) population for the most populated city for all current or historical country, and in the same row return the country's total population.

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#>
select distinct ?country ?countryLabel ?country_pop?city ?cityLabel ?city_pop
   where { 
    { select ?country (max(?city_pop) AS ?city_pop) (max(?country_pop) as ?country_pop) { {select * { ?city wdt:P31/wdt:P279* wd:Q515; wdt:P17 ?country; wdt:P1082 ?city_pop. ?country wdt:P1082 ?country_pop } }} group by ?country }
       {select * { ?city wdt:P31/wdt:P279* wd:Q515; wdt:P17 ?country; wdt:P1082 ?city_pop. ?country wdt:P1082 ?country_pop } }      service wikibase:label {bd:serviceParam wikibase:language 'en'}
  } order by desc(?city_pop)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?city"):::projected v5("?city_pop"):::projected v3("?country"):::projected v5("?country_pop"):::projected a1((" ")) a2((" ")) c7(["bd:serviceParam"]):::iri c3(["wd:Q515"]):::iri c9(["en"]):::literal v2 --"wdt:P31"--> a1 a1 --"wdt:P279"--> c3 v2 --"wdt:P17"--> v3 v2 --"wdt:P1082"--> v5 v3 --"wdt:P1082"--> v5 bind2[/"max(?city_pop)"/] v5 --o bind2 bind2 --as--o v5 bind3[/"max(?country_pop)"/] v5 --o bind3 bind3 --as--o v5 v2 --"wdt:P31"--> a2 a2 --"wdt:P279"--> c3 v2 --"wdt:P17"--> v3 v2 --"wdt:P1082"--> v5 v3 --"wdt:P1082"--> v5 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c7 --"wikibase:language"--> c9 end