query-2bd37d5a6ff7bdf7538a2c658f965948

rq turtle/ttl

male/female population must not be added unqualified as total population (!)

this is an error and should be fixed at the item using P1540 and P1539 instead

(wrong query result may be a manifestation of such)

SELECT ?year (AVG(?pop) AS ?population) ?countryLabel WHERE { ?country wdt:P31 wd:Q6256; p:P1082 ?popStatement . ?popStatement ps:P1082 ?pop; pq:P585 ?date . BIND(STR(YEAR(?date)) AS ?year)

# IF multiple ?pop values per country per year exist, we prioritize by source # census 1st, others 2nd, estimation(s) 3rd, unknown sources (none supplies P459) last # note: wikibase:rank won't help here: each year may have multiple statements for ?pop value # rank:prefered is used for the best value (or values) of the latest or current year # rank:normal may be justified for all of multiple ?pop values for a given year OPTIONAL { ?popStatement pq:P459 ?method. } OPTIONAL { ?country p:P1082 [ pq:P585 ?d; pq:P459 ?estimate ]. FILTER(STR(YEAR(?d)) = ?year). FILTER(?estimate = wd:Q791801). } OPTIONAL { ?country p:P1082 [ pq:P585 ?e; pq:P459 ?census ]. FILTER(STR(YEAR(?e)) = ?year). FILTER(?census = wd:Q39825). } OPTIONAL { ?country p:P1082 [ pq:P585 ?f; pq:P459 ?other ]. FILTER(STR(YEAR(?f)) = ?year). FILTER(?other != wd:Q39825 && ?other != wd:Q791801). } BIND(COALESCE( IF(BOUND(?census), ?census, 1/0), IF(BOUND(?other), ?other, 1/0), IF(BOUND(?estimate), ?estimate, 1/0) ) AS ?pref_method). FILTER(IF(BOUND(?pref_method),?method = ?pref_method,true)) # .. still need to group if multiple values per country per year exist and # - none is qualified with P459 # - multiple ?estimate or multiple ?census (>1 value from same source) # - ?other yields more than one source (>1 values are better than optionally # supplied estimate, but no census source available)

SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
FILTER(?year >= "2005") } GROUP BY ?year ?countryLabel ORDER BY ?year ?countryLabel

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
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/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# male/female population _must_ not be added unqualified as total population (!)
# this is an error and should be fixed at the item using P1540 and P1539 instead
# (wrong query result may be a manifestation of such)
SELECT ?year (AVG(?pop) AS ?population) ?countryLabel
WHERE
{
  ?country wdt:P31 wd:Q6256;
           p:P1082 ?popStatement .
  ?popStatement ps:P1082 ?pop;
                pq:P585 ?date .
  BIND(STR(YEAR(?date)) AS ?year)

  # IF multiple ?pop values per country per year exist, we prioritize by source
  #       census 1st, others 2nd, estimation(s) 3rd, unknown sources (none supplies P459) last
  # note: wikibase:rank won't help here: each year may have multiple statements for ?pop value
  #       rank:prefered is used for the best value (or values) of the latest or current year
  #       rank:normal may be justified for all of multiple ?pop values for a given year
  OPTIONAL { ?popStatement pq:P459 ?method. }
  OPTIONAL { ?country p:P1082 [ pq:P585 ?d; pq:P459 ?estimate ].
             FILTER(STR(YEAR(?d)) = ?year). FILTER(?estimate = wd:Q791801). }
  OPTIONAL { ?country p:P1082 [ pq:P585 ?e; pq:P459 ?census ].
             FILTER(STR(YEAR(?e)) = ?year). FILTER(?census = wd:Q39825). }
  OPTIONAL { ?country p:P1082 [ pq:P585 ?f; pq:P459 ?other ].
             FILTER(STR(YEAR(?f)) = ?year). FILTER(?other != wd:Q39825 && ?other != wd:Q791801). }
  BIND(COALESCE( 
    IF(BOUND(?census), ?census, 1/0),
    IF(BOUND(?other), ?other, 1/0),
    IF(BOUND(?estimate), ?estimate, 1/0) ) AS ?pref_method).
  FILTER(IF(BOUND(?pref_method),?method = ?pref_method,true))
  # .. still need to group if multiple values per country per year exist and
  # - none is qualified with P459
  # - multiple ?estimate or multiple ?census (>1 value from same source)
  # - ?other yields more than one source (>1 values are better than optionally
  #                         supplied estimate, but no census source available)

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }       
  FILTER(?year >= "2005")
}
GROUP BY ?year ?countryLabel
ORDER BY ?year ?countryLabel

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v8("?census") v11("?country") v2("?countryLabel"):::projected v9("?d") v14("?date") v7("?e") v10("?estimate") v5("?f") v4("?method") v6("?other") v13("?pop"):::projected v12("?popStatement") v15("?population") v15("?pref_method") v15("?year"):::projected a1((" ")) a2((" ")) a3((" ")) c12(["bd:serviceParam"]):::iri c14(["#91;AUTO_LANGUAGE#93;,en"]):::literal c6(["wd:Q6256"]):::iri f0[["?year >= '2005'"]] f0 --> v15 f1[["if(bound(?pref_method),?method = ?pref_method,'true^^xsd:boolean')"]] f1 --> v15 f1 --> v4 v11 --"p:direct/P31"--> c6 v11 --"p:P1082"--> v12 v12 --"p:statement/P1082"--> v13 v12 --"p:qualifier/P585"--> v14 bind2[/"str(year-from-dateTime(?date))"/] v14 --o bind2 bind2 --as--o v15 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v12 -."p:qualifier/P459".-> v4 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; a1 -."p:qualifier/P585".-> v9 a1 --"p:qualifier/P459"--> v10 v11 --"p:P1082"--> a1 end subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; a2 -."p:qualifier/P585".-> v7 a2 --"p:qualifier/P459"--> v8 v11 --"p:P1082"--> a2 end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; a3 -."p:qualifier/P585".-> v5 a3 --"p:qualifier/P459"--> v6 v11 --"p:P1082"--> a3 end bind3[/"if(bound(?census),?census,'1^^xsd:integer' / '0^^xsd:integer')if(bound(?other),?other,'1^^xsd:integer' / '0^^xsd:integer')if(bound(?estimate),?estimate,'1^^xsd:integer' / '0^^xsd:integer')"/] v8 --o bind3 v6 --o bind3 v10 --o bind3 bind3 --as--o v15 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c12 --"wikibase:language"--> c14 end bind5[/"average(?pop)"/] v13 --o bind5 bind5 --as--o v15