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.
Related
Let's say I'm looking for Wikidata cats who have their place of birth (P19) listed, using this query (also at https://w.wiki/5mdp):
SELECT ?item ?itemLabel ?placeofbirthLabel ?date
WHERE
{
?item wdt:P31 wd:Q146;
wdt:P19 ?placeofbirth;
schema:dateModified ?date.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
This retrieves: item id, item label, (the label of) the place of birth, and the date that any part of the item was last updated.
What if I also want to retrieve the date that the place of birth (P19) was last edited?
Thanks in advance!
Place items can also have a schema:dateModified property:
SELECT ?item ?itemLabel ?item_modified ?placeofbirthLabel ?placeofbirth_modified
WHERE
{
?item wdt:P31 wd:Q146 ;
wdt:P19 ?placeofbirth ;
schema:dateModified ?item_modified .
?placeofbirth schema:dateModified ?placeofbirth_modified .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
} LIMIT 10
I am looking for people of french nationality born in 1900 (and still living). I do not well understand the behaviour of wikidata in response to my following request:
SELECT ?item ?itemLabel ?itemDescription
WHERE {
?item wdt:P31 wd:Q5.
?item wdt:P569 ?dateOfBirth.
?item wdt:P27 wd:Q142.
FILTER NOT EXISTS {?item wdt:P570|wdt:P509|wdt:P20 ?o}
FILTER("1900-00-00"^^xsd:dateTime <= ?dateOfBirth && ?dateOfBirth < "1901-00-00"^^xsd:dateTime)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
I do not understand why the folowing request do not return itemLabel for some rows; for example the itemlabel returned for https://www.wikidata.org/wiki/Q47508624 is its "id": Q47508624
By using the wikibase:language option, you're asking for Wikidata to provide you with the labels for each ?item in the ?itemLabel variable. You've requested that it provide you labels in either the language preferred by your browser ([AUTO_LANGUAGE]) or French (fr). I would guess that your browser's default language is French also. With a browser set with English as the default, I get "Hugues Esquerre" as the ?itemLabel value for wd:Q47508624 (this record has labels defined in English and Spanish).
You can add additional acceptable languages in the comma-separated list in the query to increase the liklihood of getting label values back:
SELECT ?item ?itemLabel ?itemDescription
WHERE {
?item wdt:P31 wd:Q5.
?item wdt:P569 ?dateOfBirth.
?item wdt:P27 wd:Q142.
FILTER NOT EXISTS {?item wdt:P570|wdt:P509|wdt:P20 ?o}
FILTER("1900-00-00"^^xsd:dateTime <= ?dateOfBirth && ?dateOfBirth < "1901-00-00"^^xsd:dateTime)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,en,es". }
}
I am new to SPARQL,
Is it possible to write a query that returns Wikipedia box information for a corresponding item label from the Wikipedia box for the Arabic Language that appears at the bottom of the Wikidata item page?
see the picture:
Instead of the Wikipedia URL in the following Query, I need to return the Wikipedia Label, in our case (الرامة (جنين))
Try Query on Wikidata Query Service
SELECT DISTINCT ?article ?item ?itemLabel ?itemDescription ?entity_type ?main_category (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?item ?label "الرامة"#ar.
?item wdt:P31 ?entity_type .
MINUS { ?item wdt:P31 wd:Q4167410}
OPTIONAL{ ?item wdt:P910 ?main_category}
?article schema:about ?item;
schema:isPartOf <https://ar.wikipedia.org/>;
OPTIONAL { ?item skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "ar") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "ar" .}
}
GROUP BY ?article ?item ?itemLabel ?itemDescription ?entity_type ?main_category
This is the answer by the UninformedUser
> SELECT ?article ?wikipediaLabel WHERE
> { ?article schema:about wd:Q12187640 . ?article schema:isPartOf <https://ar.wikipedia.org/>; schema:name
> ?wikipediaLabel }
Please forgive me if I am using the wrong terminology to describe my problem.
I want to extract information on the world's island regions via WIKIDATA SPARQL query, including coordinates, the country they belong to, the archipelago they belong to and their GeoNamesIDs. Of course, this information is not provided for each and every island, so if I include it in my query, I am limiting my result list to items that already contain these properties:
SELECT ?item ?itemLabel ?coords ?GeoNamesID
WHERE {
?item wdt:P31 wd:Q23442.
?item wdt:P625 ?coords.
?item wdt:P1566 ?GeoNamesID.
?item wdt:P17 ?country.
?item wdt:P706 ?terrain.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
How can I make some of these properties "optional" to display the values if they exist but still include items that do not have them at all?
I could not find any similar issue in the long list of Wikidata SPARQL examples and would appreciate your help.
Here is my updated query including several optional properties:
SELECT ?item ?itemLabel ?coords ?GeoNamesID ?country ?continent ?terrain ?date ?named ?archipelago
WHERE {
?item wdt:P31 wd:Q23442.
?item wdt:P625 ?coords.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
OPTIONAL {?item wdt:P1566 ?GeoNamesID.}
OPTIONAL {?item wdt:P17 ?country.}
OPTIONAL {?item wdt:P30 ?continent.}
OPTIONAL {?item wdt:P706 ?terrain.}
OPTIONAL {?item wdt:P571 ?date.}
OPTIONAL {?item wdt:P138 ?named.}
OPTIONAL {?item wdt:P361 ?archipelago.}
}
I should note that I got a "time out" when first querying all optional properties. I had to retry. But it worked at once with a single optional item:
SELECT ?item ?itemLabel ?coords ?GeoNamesID
WHERE {
?item wdt:P31 wd:Q23442.
?item wdt:P625 ?coords.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
OPTIONAL {?item wdt:P1566 ?GeoNamesID.}
}
I want to use a string instead of the Wikidata ID#.
The code below works perfectly using wd:Q2263 (Tom Hanks) to show what films (Q11424) he was cast (P161) in.
SELECT DISTINCT ?item ?itemLabel ?itemDescription
WHERE {
?item wdt:P31 wd:Q11424; wdt:P161 wd:Q2263.
?article schema:about ?item;
schema:name ?title;
schema:inLanguage "en";
schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
The problem is that I want to change wd:Q2263 (Tom Hanks) to be a string like: item ?label "Tom Hanks"#en.
On my website, I am using a search box, so in the code I need the name of the person to be a string. I don't want to have to look it up in Wikipedia to get the Wikidata ID.