Reputation: 43
I use KMDb person ID to retrieve information from wikidata database. The result are three rows with three IMDB codes. If I look actress wikidata webpage (https://www.wikidata.org/wiki/Q7496795) I can see that only one of these has a reference.
How can I select only this item? Is it possible to show the reference as an output of sparql query?
Thanks a lot
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX pr: <http://www.wikidata.org/prop/reference/>
SELECT ?wikidata ?imdb ?Q ?prop ?value
WHERE {
?wikidata wdt:P345 ?imdb .
?wikidata wdt:P31 wd:Q5 .
?wikidata wdt:P1649 "00003964" . # KMDb person ID
}
LIMIT 10
Upvotes: 4
Views: 1339
Reputation: 2431
References in Wikidata use the property prov:wasDerivedFrom
to represent provenance information about statements. In your case, one way to get rid of references without value (the value node is also a statement) would be just to add a triple pattern for that:
SELECT ?wikidata ?imdb ?Q ?prop ?value
WHERE
{
?wikidata p:P345 ?statement.
?statement ps:P345 ?imdb;
prov:wasDerivedFrom ?ref .
?wikidata wdt:P31 wd:Q5.
?wikidata wdt:P1649 "00003964". # KMDb person ID
}
LIMIT 10
But you don't need to add a variable for the statement at all. As suggested by @WikidataFacts, it would be more efficient if the object of p:P345
is directly the description of the blank node:
SELECT ?wikidata ?imdb ?Q ?prop ?value
WHERE
{
?wikidata p:P345 [ ps:P345 ?imdb; prov:wasDerivedFrom ?ref ].
?wikidata wdt:P31 wd:Q5.
?wikidata wdt:P1649 "00003964". # KMDb person ID
}
LIMIT 10
There are more options, such as using explicit blank node, or filtering only nodes which have at least one value.
Upvotes: 8