Wikidata SPARQL query to return only most recent population - sparql

I have this query that returns each value of a POPULATION (property) for each POINT IN TIME (property), but I only want the most recent population for each city. Sorry for the extraneous query items.
SELECT ?cityLabel ?gps ?population ?date (SAMPLE(?areacode) as ?areacode_sample) (SAMPLE(?image) as ?image_sample)
WITH {
SELECT DISTINCT *
WHERE {
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P17 wd:Q30 .
?city wdt:P625 ?gps .
?city p:P1082 ?statement .
?statement ps:P1082 ?population .
?statement pq:P585 ?date .
OPTIONAL {?city wdt:P473 ?areacode .
}
OPTIONAL {?city wdt:P18 ?image .
}
}
ORDER BY DESC(?population)
} AS %i
WHERE {
INCLUDE %i
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
GROUP BY ?cityLabel ?gps ?population ?date
ORDER BY DESC(?population)
LIMIT 750

Related

SPARQL query timing out for WIkidata

I am trying to run a SPARQL query. As soon as I try to query schema:about for a query , the query starts timing out. I am trying to write a query to get all cities with population greater than X. Am I doing something wrong or is there a way to optimise it?
Original query which is working
SELECT DISTINCT ?city ?cityLabel ?population ?country ?countryLabel ?loc WHERE {
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1082 ?population .
?city wdt:P17 ?country .
?city wdt:P625 ?loc .
FILTER (?population >= 1000000) .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY DESC(?population)
Query to get about article:
SELECT DISTINCT ?city ?cityLabel ?population ?country ?countryLabel ?loc ?article WHERE {
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1082 ?population .
?city wdt:P17 ?country .
?city wdt:P625 ?loc .
FILTER (?population >= 1000000) .
?article schema:about ?city .
?article schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY DESC(?population)
Here is the link where I am trying to run the query: https://query.wikidata.org/

How do I create a Wikidata SPARQL query to return the top 10 US companies and their directors based on revenue?

Here is the query I am working with:
SELECT DISTINCT ?item ?itemLabel ?rev ?date (group_concat(distinct ?directorLabel ; separator = ", ") as ?directors)
WHERE {
?item wdt:P31/wdt:P279* wd:Q4830453 ;
wdt:P17 wd:Q30 .
?item p:P2139 ?revSt .
?revSt pq:P585 ?date.
FILTER ( YEAR(?date) = 2020 ).
?revSt ps:P2139 ?rev .
?item p:P3320 ?relationship.
?relationship ps:P3320 ?director.
#Filter Director by no end date
FILTER NOT EXISTS {?relationship pq:P582 ?end.}
#Group directors
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?director rdfs:label ?directorLabel .
?item rdfs:label ?itemLabel .
}
}
GROUP BY ?item ?itemLabel
ORDER BY DESC(?rev)
LIMIT 10
This query is a bad aggregate, if I add more variables to the GROUP BY clause it times out. Ultimately I would like to aggregate the wdt ids for all the directors too.
SELECT DISTINCT
?item ?itemLabel ?rev ?date
(group_concat(distinct ?directorLabel ; separator = ", ") as ?directors)
WHERE {
?item wdt:P31/wdt:P279* wd:Q4830453 ;
wdt:P17 wd:Q30 .
?item p:P2139 ?revSt .
?revSt pq:P585 ?date.
hint:Prior hint:rangeSafe "true" .
FILTER ( YEAR(?date) = 2020 ).
?revSt ps:P2139 ?rev .
?item p:P3320 ?relationship.
?relationship ps:P3320 ?director.
FILTER NOT EXISTS {?relationship pq:P582 ?end.}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?director rdfs:label ?directorLabel .
?item rdfs:label ?itemLabel .
}
}
GROUP BY ?item ?itemLabel ?rev ?date
ORDER BY DESC(?rev)
LIMIT 10

Wikidata sort cities in a given state by population

I'm looking to recreate this list of cities in Texas by population using wikidata.
I see I can do states by population with this query:
SELECT DISTINCT ?state ?stateLabel ?population
{
?state wdt:P31 wd:Q35657 ;
wdt:P1082 ?population .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?state ?population ?stateLabel
ORDER BY DESC(?population)
And the id for Texas is wd:Q1439
So I have tried the following:
SELECT ?country ?countryLabel ?state ?stateLabel ?city ?cityLabel ?population
WHERE
{
# ?state wdt:P31 wd:Q35657 . # Give me an american state
?state wdt:P31 wd:Q1439 . # that state is is Texas
?city wdt:P31 wd:Q515 . # it is an instance of city
?city wdt:P17 ?country. # get the country (for double-checking)
?city wdt:P361 ?state. # get the state it belongs to
?city wdt:P1082 ?population . # get the population of the city
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?population) limit 68
And no matches. What's the right query for this?
Update
This returns data, but is incorrect. It misses cities like Houston, San Antonio, etc.
SELECT ?mun ?munLabel ?population WHERE {
{
SELECT distinct ?mun ?population WHERE {
values ?habitation {
wd:Q3957
wd:Q515
wd:Q15284
}
?mun (wdt:P31/(wdt:P279*)) ?habitation;
wdt:P131 wd:Q1439;
# wdt:P625 ?loc;
wdt:P1082 ?population .
}
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
}
ORDER BY DESC(?population)
The issue is that Houston and San Antonio's locations are listed as Harris and Bexar county respectively, and the counties are located in Texas. If you try this query it should work:
SELECT ?mun ?munLabel ?population WHERE {
{
SELECT distinct ?mun ?population WHERE {
values ?habitation {
wd:Q3957
wd:Q515
wd:Q15284
}
?mun (wdt:P31/(wdt:P279*)) ?habitation;
wdt:P131+ wd:Q1439; #Add '+' here for transitivity
# wdt:P625 ?loc;
wdt:P1082 ?population .
}
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
}
ORDER BY DESC(?population)
The trick is to add + next to wdt:P131, which translates the query from "look for exactly one wdt:P131 edge" to "look for one or more wdt:P131 edges".
This takes care of the issue because Harris and Bexar counties are themselves listed as being located in Texas.

Wikidata Query Names of city in all languages

I query a citiy with its population from wikidata. With the language service i get he label in the given language.
But i would like to get the cityname in all languages or at least in multiple languages at once . I tried to pass * as language but i don't get all citynames returned.
Do I have to make the query for each language once ?
This is my query
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1566 "2950157" .
?city wdt:P1082 ?population .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
I also tried to just set to languages like this
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
bd:serviceparam wikibase:language "de" .
}
but it returns
Unknown error: Expected a variable in the object position to which to
bind the language.
The error is telling you that you have to bind the German tag to a variable (?cityGe) in the selection process.
SELECT DISTINCT ?city ?cityLabel ?cityGe ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1566 "2950157" .
?city wdt:P1082 ?population .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
bd:serviceParam wikibase:language "de" .
}
}
However, this does not solve your problem because this is only a fallback mechanism. If it cannot find English then it gives you German label.There are some examples here.
This can be simplified as follows with only one variable:
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de" }
However, as AKSW points out, you can use rdfs:label for your problem:
SELECT DISTINCT ?city ?label ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 .
?city wdt:P1566 "2950157" .
?city wdt:P1082 ?population .
?city rdfs:label ?label
filter(lang(?label) = 'de' || lang(?label) = 'en')
}

How to retrieve aliases from wikidata

I'm trying to retrieve some information from Wikidata and I have found interesting to collect the aliases of the voices. For examples Francesco Totti is also known as il Capitano or er Pupone :
I'm trying to retrieve all the serie a's football players with this sparql query:
SELECT ?subject ?nomeLabel ?cognomeLabel ?subjectLabel WHERE {
?subject wdt:P31 wd:Q5.
?subject p:P54 ?team .
?team ps:P54 wd:""" + team_code +""" .
FILTER NOT EXISTS { ?team pq:P582 ?end
}
OPTIONAL{
?subject wdt:P735 ?nome .
?subject wdt:P734 ?cognome .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "it". }
}
ORDER BY (?cognomeLabel)
How I can modify my query to take also the aliases?
Thanks
I have attempted a query with various labels. Here just for Roma:
SELECT distinct ?subject ?subjectLabel ?nomeLabel ?cognomeLabel ?nickname ?alternative ?subjectAltLabel WHERE {
?subject wdt:P31 wd:Q5.
?subject p:P54 ?team .
?team ps:P54 wd:Q2739 .
FILTER NOT EXISTS { ?team pq:P582 ?end . }
OPTIONAL { ?subject wdt:P735 ?nome . }
OPTIONAL { ?subject wdt:P734 ?cognome . }
OPTIONAL { ?subject wdt:P1449 ?nickname . }
OPTIONAL { ?subject skos:altLabel ?alternative . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en,fr". }
}
ORDER BY (?cognomeLabel)
I believe the P1449 property should be the most appropriate property to store an alias/nickname, but it does not seem to be used that much for football players. I just added "il Capitano" to Francesco Totti. Beyond that one there does not seem to be other nicknames for Roma players.
The "Also known as" label (in the right column) is not necessarily the nickname, but may be a spelling variation.
Something more generic if someone is interested in all properties that will return only the english also known as:
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?property a wikibase:Property .
OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
GROUP BY ?property ?propertyLabel ?propertyDescription
LIMIT 5000
Another more simple example for male actors with itemAltLabel :
#Male Actors
SELECT ?item ?itemLabel ?itemAltLabel
WHERE
{
?item wdt:P21 wd:Q6581097.
?item wdt:P106 wd:Q33999.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}