Wikidata+SPARQL: lookup a company based on it's ticker symbol

倖福魔咒の 提交于 2019-12-01 09:47:39

So the answer based on AKSW and Stanislav's comments are that THIS query will list all the stocks on the New York Stock Exchange (as long as the ticker is listed 'under' the exchange:

SELECT DISTINCT ?id ?idLabel ?exchange ?ticker
WHERE {
    ?id wdt:P31/wdt:P279* wd:Q4830453 .
    ?id p:P414 ?exchange . 
    ?exchange ps:P414 wd:Q13677 .
    ?exchange pq:P249 ?ticker .

    ?id rdfs:label ?idLabel 
    FILTER(LANG(?idLabel) = 'en').
}

And THIS query will find a specific stock (IBM) on the New York stock exchange:

SELECT DISTINCT ?id ?idLabel ?exchange ?ticker
WHERE {
    ?id wdt:P31/wdt:P279* wd:Q4830453 .
    ?id p:P414 ?exchange . 
    ?exchange ps:P414 wd:Q13677 .
    ?exchange pq:P249 ?ticker . FILTER(LCASE(STR(?ticker)) = 'ibm') .

    ?id rdfs:label ?idLabel 
    FILTER(LANG(?idLabel) = 'en').
}

And THIS query will find a specific stock on ANY stock exchange, OR referenced directly (Shown here with two different stock tickers to illustrate the search). This query is quite long because wikidata sometimes have the stock exchange sub field UNDER the ticker, and sometimes the other way around. Oh, and sometimes they are two different fields altogether (not linked). Oh Joy.

SELECT DISTINCT ?id ?idLabel ?exchange ?exchangeLabel ?ticker
WHERE {
    ?id wdt:P31/wdt:P279* wd:Q4830453 .
    { 
       # Find cases where the ticker is the main attribute, and the exchange may be below it.
       ?id wdt:P249 ?ticker . FILTER(LCASE(STR(?ticker)) = 'nsu') .
       ?id p:P249 ?tickersub .
       ?tickersub pq:P414 ?exchange 
    }
    UNION {
       # Find the exchange and it's ticker 
       ?id wdt:P414 ?exchange . 
       ?id p:P414 ?exchangesub .
       ?exchangesub pq:P249 ?ticker . FILTER(LCASE(STR(?ticker)) = 'ibm') .
    } 
    UNION {
       # Find the exchange and it's ticker 
       ?id wdt:P414 ?exchange . 
       ?id wdt:P249 ?ticker . FILTER(LCASE(STR(?ticker)) = 'frme') .
    } 
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!