I'm trying to write a SPARQL-query for wikidata, returning all European countries with their most recent properties (population, area, average income, age of retirement and human development index). One country should fill one line because I would like to cluster this data later.
I wrote the following query but it didn't work as expected.
SELECT ?countryLabel ?population ?populationDate ?area ?areaDate ?income ?incomeDate ?retirementAge ?retirementAgeDate ?hdi ?hdiDate WHERE {
?country wdt:P31 wd:Q6256.
OPTIONAL {
SELECT ?country (MAX(?popDate) AS ?latestPopDate) ?population WHERE {
?country p:P1082 ?popStmt.
?popStmt ps:P1082 ?population.
?popStmt pq:P585 ?popDate.
} GROUP BY ?country ?population
}
OPTIONAL {
SELECT ?country (MAX(?areaDate) AS ?latestAreaDate) ?area WHERE {
?country p:P2046 ?areaStmt.
?areaStmt ps:P2046 ?area.
?areaStmt pq:P585 ?areaDate.
} GROUP BY ?country ?area
}
OPTIONAL {
SELECT ?country (MAX(?incomeDate) AS ?latestIncomeDate) ?income WHERE {
?country p:P3529 ?incomeStmt.
?incomeStmt ps:P3529 ?income.
?incomeStmt pq:P585 ?incomeDate.
} GROUP BY ?country ?income
}
OPTIONAL {
SELECT ?country (MAX(?retirementAgeDate) AS ?latestRetirementAgeDate) ?retirementAge WHERE {
?country p:P8772 ?retirementAgeStmt.
?retirementAgeStmt ps:P8772 ?retirementAge.
?retirementAgeStmt pq:P585 ?retirementAgeDate.
} GROUP BY ?country ?retirementAge
}
OPTIONAL {
SELECT ?country (MAX(?hdiDate) AS ?latestHdiDate) ?hdi WHERE {
?country p:P1081 ?hdiStmt.
?hdiStmt ps:P1081 ?hdi.
?hdiStmt pq:P585 ?hdiDate.
} GROUP BY ?country ?hdi
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?countryLabel
How would you write such a query?