I would like to know the most recent population of Swiss municipalities with the help of Wikidata. To get the ones which have the preferred rank would be easy but not helpful. I have the following SPARQL query but now I have the problem to project the actual population count ?popWD
for the latest date (and not only the date) out of the aggregation. As I understand, you cannot project values which are not aggregated.
SELECT ?wikiURI (max(?timeWDAll) AS ?timeWD) WHERE {
?wikiURI wdt:P31 wd:Q70208;
p:P1082 ?popWDStatement.
?popWDStatement pq:P585 ?timeWDAll;
ps:P1082 ?popWD.
}
GROUP BY ?wikiURI
I solved the problem with a subselect which does work but seems a little bit complicated and quite slow:
SELECT ?wikiURI ?timeWD ?popWD WHERE {
?wikiURI p:P1082 ?popWDStatement.
?popWDStatement ps:P1082 ?popWD;
pq:P585 ?timeWD.
{
SELECT ?wikiURI (max(?timeWDAll) AS ?timeWD) WHERE {
?wikiURI wdt:P31 wd:Q70208;
p:P1082 ?popWDStatement.
?popWDStatement pq:P585 ?timeWDAll.
}
GROUP BY ?wikiURI
}
}
Are there better and faster solutions? The code snippets above should both work at: https://query.wikidata.org