0

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
logi-kal
  • 7,107
  • 6
  • 31
  • 43
BenP
  • 825
  • 1
  • 10
  • 30
  • 1
    what exactly does not work with the query? getting the cities works, or not? – UninformedUser Jun 11 '19 at 10:55
  • 1
    what is the purpose of `?hqItem ps:P159 wd:Q515.` ? if you try to filter for cities, the relation `ps:P159` is clearly wrong. In Wikidata, "instance of" relation is `wdt:P31` – UninformedUser Jun 11 '19 at 11:06
  • 2
    `SELECT ?item ?itemLabel ?web ?isin ?hqLabel ?hqloc ?hqCountry ?hqCountryLabel ?inception WHERE { #values ?item {wd:Q74687} # for debugging ?item p:P31/ps:P31/wdt:P279* wd:Q783794. OPTIONAL{?item wdt:P856 ?web.} # get website OPTIONAL{?item wdt:P946 ?isin.} # get ISIN OPTIONAL{?item wdt:P571 ?inception.} # get inception date` – UninformedUser Jun 11 '19 at 11:21
  • 2
    `OPTIONAL{?item p:P159 ?hqStmt. # get HQ statement ?hqStmt ps:P159 ?hq. # get HQ item ?hqStmt pq:P625 ?hqloc. # get HQ coordinate location qualifier optional {?hq wdt:P17 ?hqCountry} # optional, the country of the HQ } ?article schema:about ?item . ?article schema:inLanguage "en" . ?article schema:isPartOf . SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } } LIMIT 10` – UninformedUser Jun 11 '19 at 11:21
  • 1
    is this what you want? – UninformedUser Jun 11 '19 at 11:21
  • Appreciate your suggestions. – BenP Jun 14 '19 at 12:44

1 Answers1

5

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.

np00
  • 218
  • 2
  • 6