Not getting the aggregated results I'm looking from Wikidata sparql query - sparql

I'm trying to get some data about Disneyland's hotels and I'm stuck here with architects. On of the hotels has two architects. As you can see I've tried to concatinate architects and group othe selected items.
But I'm not getting their labels instead I'm getting their URI. What am I missing?
SELECT DISTINCT ?hotel ?hotelLabel ?openingDateLabel ?countryLabel
(group_concat(?architects;separator=", ") as ?architect)
WHERE
{
?hotel wdt:P31 wd:Q27686; # vi leder efter noget, som er "instance of" et hotel
wdt:P361 wd:Q206521; # og som er "part of" Disneyland Paris
wdt:P1619 ?openingDate;
wdt:P495 ?country;
wdt:P84 ?architects.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?hotel rdfs:label ?hotelLabel .
?openingDate rdfs:label ?openingDateLabel .
?country rdfs:label ?countryLabel .
?architect rdfs:label ?architectLabel .
}
}
Group by ?hotel ?hotelLabel ?openingDateLabel ?countryLabel

Related

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.

Query Wikidata countries - how to return labels in all languages

How do I query Wikidata countries in order to return the label of all countries in all available languages?
I am able to run the query by specifying the languages I want (e.g. I successfully asked for a list of countries with the English and Arabic labels). But can I ask it to return a label column for all languages? If so, how?
Countries in English and Arabic
SELECT ?country ?label_en ?label_ar
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label_en filter (lang(?label_en) = "en").
?country rdfs:label ?label_ar filter (lang(?label_ar) = "ar").
}
Sparql Query link
As #AKSW said in a comment, you can get the list of all country names in all languages by removing the filter from your query:
SELECT ?country ?label
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label
}
You might also (perhaps) want to know the identity of each language being returned, and the English word for each country being named:
SELECT ?country ?label (lang(?label) as ?label_lang) ?countryLabel
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Wikidata SPARQL - get company entities and the location of their headquarters

I'm having trouble extracting location attributes of company HQ's.
My query: finds all companies or sub-classes, and returns some basic properties such as ISIN and URL, and the Headquarter location.
I have tried to use this example to extend the Headquarter part of the query to return location information such as city, country, and coordinate latitude and longitude. However I am getting stuck on pulling the values or labels through.
Thank you
SELECT
?item ?itemLabel ?web ?isin ?hq ?hqloc ?inception
# valueLabel is only useful for properties with item-datatype
WHERE
{
?item p:P31/ps:P31/wdt:P279* wd:Q783794.
OPTIONAL{?item wdt:P856 ?web.} # get item
OPTIONAL{?item wdt:P946 ?isin.} # get item
OPTIONAL{?item wdt:P571 ?inception.} # get item
OPTIONAL{?item wdt:P159 ?hq.}
OPTIONAL{?item p:P159 ?hqItem. # get property
?hqItem ps:P159 wd:Q515. # get property-statement wikidata-entity
?hqItem pq:P17 ?hqloc. # get country of city
}
?article schema:about ?item .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
A more simplified query to select some of the values you mentioned:
SELECT
?company ?companyLabel ?isin ?web ?country ?countryLabel ?inception
WHERE
{
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
?article schema:about ?company .
?company p:P31/ps:P31/wdt:P279* wd:Q783794.
?company wdt:P946 ?isin.
OPTIONAL {?company wdt:P856 ?web.}
OPTIONAL {?company wdt:P571 ?inception.}
OPTIONAL {?company wdt:P17 ?country.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} LIMIT 10
What I changed:
changed some labels to be more explicit (ex: "?item" -> "?company")
usage of P17 to directly select the country
I removed the OPTIONAL on ISIN to show that there exist some values. You did not get a result because it seems that many company instances on Wikidata lack that information.
From here, selecting the other values should be easy.

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