问题
I have a query to get all Capitals. (Capital Cities)
SELECT DISTINCT ?Stadt ?label ?Staat ?StaatLabel ?geographische_Koordinaten ?StadtLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Stadt wdt:P31 wd:Q5119.
?Stadt rdfs:label ?label.
OPTIONAL { ?Stadt wdt:P17 ?Staat. }
OPTIONAL { ?Stadt wdt:P625 ?geographische_Koordinaten. }
}
LIMIT 100
Try it here
and the result is:
wd:Q61 Washington D.C. wd:Q30 Vereinigte Staaten Point(-77.036666666 38.895) Washington, D.C.
wd:Q61 Washington D. C. wd:Q30 Vereinigte Staaten Point(-77.036666666 38.895) Washington, D.C.
wd:Q61 واشنگٹن ڈی سی wd:Q30 Vereinigte Staaten Point(-77.036666666 38.895) Washington, D.C.
wd:Q61 Washington D.C. wd:Q30 Vereinigte Staaten Point(-77.036666666 38.895) Washington, D.C.
wd:Q61 واشنګټن ډي سي wd:Q30 Vereinigte Staaten Point(-77.036666666 38.895) Washington, D.C.
wd:Q61 Вашингтон wd:Q30 Vereinigte Staaten Point(-77.036666666 38.895) Washington, D.C.
wd:Q61 ওয়াশিংটন, ডি.সি. wd:Q30 Vereinigte Staaten Point(-77.036666666 38.895) Washington, D.C.
I get many results of the same city in several languages.
How can I filter the query so I get only one result per city with the english label ?
* UPDATED *
Thanks for helping Stanislav. You answered the question. My new query is:
SELECT DISTINCT ?Stadt ?label ?StadtLabel ?Staat ?StaatLabel ?geographische_Koordinaten WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
?Stadt wdt:P31 wd:Q5119.
?Stadt wdt:P17 ?Staat.
OPTIONAL { ?Stadt wdt:P625 ?geographische_Koordinaten. }
}
LIMIT 100
Try it here
May I ask a following question ? Why do I get not only the capitals with this query ? Why "?Stadt wdt:P31 wd:Q5119." is ignored ?
回答1:
Following @Stanislav Kralin's commnet and the examples here, this query returns countries and their capitals:
SELECT ?country ?countryLabel ?capital ?capitalLabel ?coords WHERE {
?country wdt:P31 wd:Q3624078.
?country wdt:P36 ?capital.
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} # not a former country
OPTIONAL { ?capital wdt:P625 ?coords. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?countryLabel
LIMIT 1000
Try it here
However some countries such as Bolivia, Costa Rica and South Africa have more than one capital for some reasons!
To limit each country to one (random) capital, try:
SELECT ?country ?countryLabel ?aCapital ?aCapitalLabel ?coords
WHERE
{ { { SELECT ?country (SAMPLE(?capital) AS ?aCapital)
WHERE
{ ?country wdt:P31 wd:Q3624078
FILTER NOT EXISTS { ?country wdt:P31 wd:Q3024240 }
?country wdt:P36 ?capital
}
GROUP BY ?country
}
OPTIONAL
{ ?aCapital wdt:P625 ?coords }
SERVICE wikibase:label
{ bd:serviceParam
wikibase:language "en"
}
}
}
ORDER BY ?countryLabel
LIMIT 1000
Try it here
来源:https://stackoverflow.com/questions/50413677/how-to-filter-results-of-wikidata-to-specific-language