Wikidata query - how to add year to city's population? - sparql

I would like to get the year of population count for each city.
Do tou know how to add it correctly? Currently I got empty results.
Here's my query:
SELECT DISTINCT ?cityLabel ?population ?gps ?data WHERE {
?city (wdt:P31/(wdt:P279*)) wd:Q515;
wdt:P1082 ?population;
wdt:P625 ?gps.
OPTIONAL { ?population wdt:P585 ?date. } # here I have a problem
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population) LIMIT 100
PS. just paste it here: https://query.wikidata.org/

First problem: you are selecting ?data but the actual variable is ?date.
Second problem: ?population is the object of your statement, but qualifiers refer to a whole statement, not just its object.
For referring to the statement, you'll have to use p:P1082 instead of wdt:P1082.
You can obtain what you want with the following query:
SELECT DISTINCT ?cityLabel ?population ?gps ?date WHERE {
?city
wdt:P31/wdt:P279* wd:Q515;
wdt:P625 ?gps.
?city p:P1082 ?populationStatement .
?populationStatement ps:P1082 ?population .
OPTIONAL { ?populationStatement pq:P585 ?date. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population)
LIMIT 10
I set LIMIT 10 because this is a pretty heavy query and sometimes it reaches timeout.
To deepen the topic, I'd suggest you to read Wikidata:SPARQL tutorial§Qualifiers.

Related

Retrieve latitude and longitude of a sample of coordinates from Wikidata using SPARQL

I am trying to retrieve samples of coordinates in Wikidata via SPARQL but am having a very difficult time trying to achieve it. I would want to get only a single pair of coordinates per place and display the result in a column, and the latitude and longitude of the said coordinates sample in their own columns.
The following code (link to WQS) I use below works, but it does not get the coordinates values labels in Point(5.936111111 51.21) format. When I replace p:P625 with wdt:P625, no items are retrieved. Additionally, Borculo (Q1025685) appears twice in the results with two unique coordinates:
SELECT DISTINCT ?place ?placeLabel (SAMPLE(?temp1) AS ?coords_sample) ?lat ?long {
?place p:P31 ?instanceOf.
?instanceOf ps:P31/wdt:279* wd:Q2039348.
?place p:P625 ?temp1.
?temp1 psv:P625 ?temp2.
?temp2 wikibase:geoLatitude ?lat.
?temp2 wikibase:geoLongitude ?long.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} GROUP BY ?place ?placeLabel ?lat ?long
ORDER BY ?placeLabel
Use ps:P625 for obtaining the coordinates in the desired format (see also the manual on Wikibooks).
Also, it is not sufficient to sample the coordinates statement if you also group by ?lat and ?long. Hence, you'd better to sample it in a subquery.
Final result:
SELECT DISTINCT ?place ?placeLabel ?coords ?lat ?long {
?place p:P31/ps:P31/wdt:279* wd:Q2039348 ;
p:P625 ?coords_sample .
{
SELECT (SAMPLE(?coords_stmt) AS ?coords_sample) {
?place p:P31/ps:P31/wdt:279* wd:Q2039348 ;
p:P625 ?coords_stmt .
} GROUP BY ?place
}
?coords_sample ps:P625 ?coords;
psv:P625 [
wikibase:geoLatitude ?lat;
wikibase:geoLongitude ?long
] .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?placeLabel

Wikidata: Filter post codes only current valid

I would like to filter post codes to show only the current active for today.
Problem is there are are cities with old post codes (Example).
My current query shows the old post codes:
SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
wdt:P17 wd:Q183;
wdt:P281 ?postcode;
wdt:P131 ?federal_state.
?federal_state wdt:P439 ?federal_state_nr.
}
ORDER BY (?postcode)
LIMIT 10
(query.wikidata.org)
I would have to use start time P580 and end time P582 but I don't see how.
You can use this for filtering out the claims which have an end time:
?city p:P281 ?postCodeStmt .
?postCodeStmt ps:P281 ?postcode .
FILTER NOT EXISTS { ?postcode pq:P582 ?endTime . }
The whole query becomes:
SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
wdt:P17 wd:Q183;
p:P281 ?postCodeStmt;
wdt:P131 ?federal_state.
?federal_state wdt:P439 ?federal_state_nr.
?postCodeStmt ps:P281 ?postcode.
FILTER NOT EXISTS { ?postCodeStmt pq:P582 ?endTime . } # Filtering out old postal codes
}
ORDER BY (?postcode)
LIMIT 100
See also Wikidata:SPARQL tutorial§Qualifiers.

Return cities in Wikidata SPARQL Query, similar to a Wikipedia page

I'm not sure what I'm doing wrong. I have a nice list, but not only are the cities duplicating, but I'm unsure how they're defined as cities. I would expect to see London in the results and have similar results to this Wikipedia page. These results are quite different to the Wikipedia page.
I want to:
Get a list of cities, with their first-level administrative country subdivision (province/state/region), similar to this Wikipedia page
While avoiding duplicate cities.
SELECT ?city ?cityLabel ?country ?population ?countryLabel ?region ?regionLabel ?lat ?long
WHERE
{
?city wdt:P31/wdt:P279 wd:Q515 . # find instances of subclasses of city
?city (wdt:P131) ?region.
?region wdt:P31/wdt:P279 wd:Q10864048 .
?city wdt:P1082 ?population .
?city wdt:P17 ?country . # Also find the country of the city
?city p:P625 ?statement . # coordinate-location statement
?statement psv:P625 ?coordinate_node .
OPTIONAL { ?coordinate_node wikibase:geoLatitude ?lat. }
OPTIONAL { ?coordinate_node wikibase:geoLongitude ?long.}
FILTER (?population > 100000) .
# choose language
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 8000
Try it
Update:
Although not an answer to this specific question, anyone trying to get similar data to this should have a look here.
Update 2:
With help in the comments from #UninformedUser, the query is now:
SELECT DISTINCT ?city ?cityLabel ?country ?population ?countryLabel ?region ?regionLabel ?lat ?long
WHERE
{
?city wdt:P31/wdt:P279 wd:Q515 . # find instances of subclasses of city
?city (wdt:P131) ?region.
?region wdt:P31/wdt:P279 wd:Q10864048 .
?city p:P1082 ?populationStmt .
?populationStmt ps:P1082 ?population ; pq:P585 ?pop_date .
?city wdt:P17 ?country . # Also find the country of the city
?city p:P625 ?statement . # coordinate-location statement
?statement psv:P625 ?coordinate_node .
OPTIONAL { ?coordinate_node wikibase:geoLatitude ?lat. }
OPTIONAL { ?coordinate_node wikibase:geoLongitude ?long.}
FILTER NOT EXISTS {
?city p:P1082/pq:P585 ?pop_date_ .
FILTER (?pop_date_ > ?pop_date)
}
FILTER (?population > 100000) .
# choose language
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 8000
Try it

How to SPARQL select all living politicians from EU political parties on Wikidata?

Can somebody, please help me write a SPARQL query, which would select all currently living politicians, whose political institutions are from a country within the EU?
Currently I have 3 queries, but I don't know how to combine them
The first one checks for people who are living, whose occupation is politician and/or who are a member of a political party
SELECT DISTINCT ?politician ?politicianLabel
WHERE {
?politician wdt:P106 wd:Q82955 .
FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}
OPTIONAL {
?politician wdt:P102 ?membership .
}
FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
The second one shows all EU countries
SELECT ?country ?countryLabel
WHERE {
?country wdt:P463 wd:Q458
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
And the third one finds parties from a specific country
SELECT ?party
WHERE {
?party wdt:P31 wd:Q7278
?party wdt:17 #EU Countries#
}
But How can I combine these queries? Can somebody help?
Thank you
Thank you #AKSW
This works:
WHERE {
?country wdt:P463 wd:Q458.
?party wdt:P31 wd:Q7278 .
?party wdt:P17 ?country .
?politician wdt:P106 wd:Q82955 . hint:Prior hint:runFirst true.
?politician wdt:P102 ?party .
FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}
}
If you start with the country and add the
hint:Prior hint:runFirst true.
line, it is optimized enough to give result.

SPARQL bordering countries example

Can anyone show me any SPARQL query to get all bordering contries of all countries from http://www4.wiwiss.fu-berlin.de/factbook/sparql?
For example Afghanistan has:
factbook:landboundary db:China,
factbook:landboundary db:Iran,
factbook:landboundary db:Pakistan,
factbook:landboundary db:Tajikistan,
factbook:landboundary db:Turkmenistan
My try of getting data:
SELECT ?country ?name ?neighbour
WHERE {
?country rdf:type factbook:Country .
?country rdfs:label ?name.
OPTIONAL{
?country factbook:landboundary ?neighbour.
}
}
ended with following message:
rethrew: de.fuberlin.wiwiss.d2rq.D2RQException: Table 'factbook.neighbors' doesn't exist: SELECT DISTINCT `T0_neighbors`.`name_encoded` FROM `bordercountries` AS `T0_bordercountries`, `neighbors` AS `T0_neighbors`, `countries` AS `T0_countries` WHERE `T0_bordercountries`.`Landboundaries_bordercountries_title` = `T0_neighbors`.`Name` AND `T0_bordercountries`.`Name` = `T0_countries`.`Name` AND `T0_countries`.`name_encoded` = 'Aruba' (E0)
I've asked the same question on http://answers.semanticweb.com but no luck yet so I'm trying my luck here
The failure seems to be caused by an internal system error. Your SPARQL query does not have any syntax errors and the predicates you provided are valid according to the data.
However, I don't understand how your query is supposed to return the neighbors of one specific country. Maybe you want to try something like this:
SELECT DISTINCT ?neighbor
WHERE {
?neighbor rdf:type factbook:Country .
?neighbor factbook:landboundary db:Afghanistan .
}
Very much later and not exactly an answer to this question (the SPARQL Endpoint to the CIA Factbook seem to be down at the moment), but WikiData has a few examples how to get bordering countries according to their data set at https://query.wikidata.org/
E.g. if you open the examples and search for "border", you get a query for "countries sharing a border with Cameroon":
#Population of countries sharing a border with Cameroon
#defaultView:LineChart
SELECT ?country ?year ?population ?countryLabel WHERE {
{
SELECT ?country ?year (AVG(?population) AS ?population) WHERE {
{
SELECT ?country (str(YEAR(?date)) AS ?year) ?population WHERE {
?country wdt:P47 wd:Q1009; # shares border with Cameroon
p:P1082 ?populationStatement.
?populationStatement ps:P1082 ?population;
pq:P585 ?date.
}
}
}
GROUP BY ?country ?year
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
To understand this one has to know (or figure out) that wd:Q1009 is actually Cameroon. Not sure how to do this.
This example also displays a - imo not very useful - display of the population of the surrounding countries by year.
A simpler version without the extra data is:
SELECT ?country ?countryLabel WHERE {
?country wdt:P47 wd:Q1009 # shares border with Cameroon
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
}
(The SERVICE wikibase:label is a WikiData extension)
Finally all bordering neighbours for all countries might be:
SELECT ?country ?countryLabel ?neighbourLabel ?neighbour WHERE {
?country wdt:P31 wd:Q6256;
wdt:P47 ?neighbour
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
} ORDER BY ?countryLabel ?neighbourLabel