I want to write a SPARQL query that gives me the wikidata_id, label, stock exchange, and ticker symbol for all instances of a company being listed on a stock exchange.
My query so far looks like
SELECT DISTINCT ?id ?idLabel ?exchangeLabel ?tickerLabel
WHERE {
?id wdt:P31/wdt:P279* wd:Q783794 ;
wdt:P414 ?exchange ;
p:P414 [pq:P249 ?ticker].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
While this produces results that almost seem right, there is a problem when companies are listed on multiple exchanges -- here's an example of the problem in the results:
Note how in the above, Credit Suisse is listed three times, with three different tickers. While it's correct that Credit Suisse is listed on three stock exchanges, the problem is that the NYSE is listed as the exchange in all three cases. Even worse, there are in fact nine rows for Credit Suisse, associating every listing with every stock exchange. The correct listing info would contain only three listings, and is provided on Credit Suisse's wikidata page:
What am I doing wrong? How can I get the correct exchange to be associated with each ticker's row?