query-f7c22482268383cad052c0aa9241ab3c

rq turtle/ttl

Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459) Propertiesinstance of (P31)population (P1082)point in time (P585)determination method or standard (P459)

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#>
#defaultView:BarChart
# 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
       (COUNT(*) AS ?number_of_chosen_sources) (SAMPLE(?method) AS ?a_source_of_those_chosen)
WHERE
{
  ?country wdt:P31 wd:Q3624078;#more useful than Q6256;
           p:P1082 ?popStatement .
  ?popStatement ps:P1082 ?pop;
                pq:P585 ?date .
  BIND(STR(YEAR(?date)) AS ?year)
  #FILTER ( (YEAR(?date)) >= 2000 ) 
  # 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" }               
}
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; v17("?a_source_of_those_chosen") v8("?census") v11("?country") v2("?countryLabel"):::projected v9("?d") v14("?date") v7("?e") v10("?estimate") v5("?f") v4("?method"):::projected v16("?number_of_chosen_sources") v6("?other") v13("?pop"):::projected v12("?popStatement") v15("?population") v15("?pref_method") v15("?year"):::projected a1((" ")) a2((" ")) a3((" ")) c5(["wd:Q3624078"]):::iri c11(["bd:serviceParam"]):::iri c13(["#91;AUTO_LANGUAGE#93;,en"]):::literal f0[["if(bound(?pref_method),?method = ?pref_method,'true^^xsd:boolean')"]] f0 --> v15 f0 --> v4 v11 --"p:direct/P31"--> c5 v11 --"p:P1082"--> v12 v12 --"p:statement/P1082"--> v13 v12 --"p:qualifier/P585"--> v14 bind1[/"str(year-from-dateTime(?date))"/] v14 --o bind1 bind1 --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 bind2[/"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 bind2 v6 --o bind2 v10 --o bind2 bind2 --as--o v15 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c11 --"wikibase:language"--> c13 end bind6[/"average(?pop)"/] v13 --o bind6 bind6 --as--o v15 bind7[/"count(*)"/] bind7 --as--o v16 bind8[/"sample(?method)"/] v4 --o bind8 bind8 --as--o v17