Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wikidata+SPARQL: Get tickers of all companies listed on stock exchanges

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: enter image description here 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: enter image description here

What am I doing wrong? How can I get the correct exchange to be associated with each ticker's row?

like image 453
conradlee Avatar asked Jan 22 '26 08:01

conradlee


1 Answers

Thanks to @StansilavKralin (in a comment to my question) I can provide an answer:

SELECT DISTINCT ?id ?idLabel ?exchangeLabel ?tickerLabel
WHERE {
  ?id wdt:P31/wdt:P279* wd:Q783794 ; p:P414 [pq:P249 ?ticker; ps:P414 ?exchange ] .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
like image 91
conradlee Avatar answered Jan 25 '26 22:01

conradlee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!