Want to extract Bob Dylan pseudonyms. His Wikidata page is https://www.wikidata.org/wiki/Q392
One way is to walk the following tree and parse value for P742 (pseudonym): https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q392&format=json
But that's a lot of work and specific to Bob Dylan; other people with pseudonyms may have a different JSON structure.
This query works for his awards:
SELECT ?item ?itemLabel ?linkTo ?linkToLabel {
?item wdt:P166 wd:Q37922 ;
wdt:P910 wd:Q8064684 .
OPTIONAL {
?item wdt:P166 ?linkTo ;
wdt:P31 wd:Q5 } .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
as seen here
The weakness is it uses the combination of "topic's main category" (P910) and the target "category" (Q8064684) to specify "Bob Dylan".
For that query, I can't find the method to use his QID (Q392).
I do not want to text search, as is proposed in Group concat not working, because there is more than one Bob Dylan. And many people with pseudonyms will not have a category, so that technique cannot be generalized.
This query works for occupations and uses his QID (Q392), not the Bob Dylan category (Q8064684) from the "awards" query.
SELECT * {
SERVICE <https://query.wikidata.org/sparql> {
wd:Q392 wdt:P31 wd:Q5 ;
wdt:P106 ?occupation
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?occupation rdfs:label ?label
}
}
}
as seen here
but I cannot find the right SPARQL query to select pseudonyms (which are text) as seen here:
https://www.wikidata.org/wiki/Q392
How to write a Wikidata SPARQL query that uses a person's identifier (e.g. Q392 for Bob Dylan) and can be configured (easily toggled) to select either "award received" (P166), "occupations" (P106) or "pseudonym" (P742)?
Related
I am trying to construct a query to return a list of actors and their spouses while including marriage and divorce dates for each couple. So I would expect to see each actor duplicate with each instance of a new relationship... however when I try and include the start time and end time properties in the query, I am getting duplicate results. I suspect this is because the "name" of the spouses and the is stored in a different wikidata prefix and I'm not grouping them correctly.
Here is a sample query:
SELECT ?person ?personLabel ?spouse ?spouseLabel ?starttime ?endtime
WHERE
{
?person wdt:P106 wd:Q33999, wd:Q2526255, wd:Q28389, wd:Q3282637;
wdt:P26 ?spouse.
?person p:P26 [pq:P580 ?starttime; pq:P582 ?endtime].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC(UCASE(str(?personLabel)))
LIMIT 10
here is a link to the sparql interactive service so you can see the duped results I'm referring to:
https://query.wikidata.org/#SELECT%20%3Fperson%20%3FpersonLabel%20%3Fspouse%20%3FspouseLabel%20%3Fstarttime%20%3Fendtime%0AWHERE%0A%7B%0A%20%20%3Fperson%20wdt%3AP106%20wd%3AQ33999%2C%20wd%3AQ2526255%2C%20wd%3AQ28389%2C%20wd%3AQ3282637%3B%0A%20%20%20%20%20%20%20%20%20%20wdt%3AP26%20%3Fspouse.%0A%20%20%3Fperson%20p%3AP26%20%5Bpq%3AP580%20%3Fstarttime%3B%20pq%3AP582%20%3Fendtime%5D.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22.%20%7D%0A%7D%0AORDER%20BY%20ASC%28UCASE%28str%28%3FpersonLabel%29%29%29%0ALIMIT%2010%0A
screencap of duped results
The problem with your query is that there was no link between the spouse and the statement about their marriage.
So for every actor, you are returning all their spouses, and also all the start/end dates of their marriages, regardless of whether they relate to the specific spouse.
What you need to do is to use the ps: namespace, like so:
SELECT ?person ?personLabel ?spouse ?spouseLabel ?starttime ?endtime
WHERE
{
?person wdt:P106 wd:Q33999, wd:Q2526255, wd:Q28389, wd:Q3282637 .
?person p:P26 [ ps:P26 ?spouse ; #This is the necessary change.
pq:P580 ?starttime;
pq:P582 ?endtime ].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC(?personLabel)
LIMIT 10
In general, the wdt: namespace is for linking entities directly, the p: namespace links an entity to a statement, ps: links a statement to an entity, and pq: tells us something about the statement.
I want to retrieve all instances of musicians (Q639669) in a given city (P131) born after 1900. When I pass in the wikidata example city Rotterdam (Q34370) it works. However, replacing the city with a larger city (e.g., Paris, Q90) it will timeout.
Is there a way to optimise this or split into chunks to make repeated queries?
I'm actually only interested the number of cases it returns (i.e. a single value), without needing all the metadata about the artist name, etc.
Would be really helpful if someone can give me pointers to solving this. Thanks!
SELECT ?itemLabel ?itemDescription ?birth
WHERE {
?item wdt:P106/wdt:P279* wd:Q639669 .
?item wdt:P19/wdt:P131* wd:Q34370 .
OPTIONAL {?item wdt:P569 ?birth}
filter (?birth > "1900-01-01"^^xsd:dateTime)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
The * (ZeroOrMore) property path operator used on the P131 ("located in the administrative territorial entity") is one of the culprits here. A simple approach to getting an answer would be to manually run queries that build up that property path one element at a time:
In query 1: ?item wdt:P19 wd:Q34370 .
In query 2: ?item wdt:P19/wdt:P131 wd:Q34370 .
In query 3: ?item wdt:P19/wdt:P131/wdt:P131 wd:Q34370 .
etc.
I found through experimentation that there is no data past 3 occurrences of P131. However, be aware that there are duplicates across these queries, because some people are listed as having birth places both "in Paris" and also in some sub-region of Paris (for example, Claude Arrieu (Q272886) listed as being born in both Paris and the 8th arrondissement).
You can also use UNION to put several of these property paths together into a single query, though be aware that this may increase the query time and move you back towards a timeout depending on the data:
SELECT ?item ?itemLabel ?itemDescription ?birth WHERE {
?item wdt:P106 / wdt:P279 * wd:Q639669 .
{
?item wdt:P19 wd:Q90 .
} UNION {
?item wdt:P19 / wdt:P131 wd:Q90 .
} UNION {
?item wdt:P19 / wdt:P131 / wdt:P131 wd:Q90 .
} UNION {
?item wdt:P19 / wdt:P131 / wdt:P131 / wdt:P131 wd:Q90 .
} UNION {
?item wdt:P19 / wdt:P131 / wdt:P131 / wdt:P131 / wdt:P131 wd:Q90 .
}
OPTIONAL {
?item wdt:P569 ?birth
}
FILTER(?birth > "1900-01-01"^^xsd:dateTime)
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
}
}
A couple of other comments:
If you only want the count of people, you can replace the SELECT variables with a count, which may improve the runtime a bit: SELECT (COUNT(DISTINCT ?item) AS ?count) WHERE { … } which returns the answer 1440.
The use of OPTIONAL to bind the ?birth variable combined with the FILTER outside of the OPTIONAL may not be what you want. The Filter will remove any results where ?birth is unbound, making the OPTIONAL really non-optional. Consider either removing the OPTIONAL and binding ?birth right next to the FILTER, or moving the FILTER inside the OPTIONAL to apply that date range filter only to people who have birth data (which changes the count from 1440 to 2456 – many musicians born in Paris missing birth dates, it seems!)
I am trying to get the label of a wikidata item using its id. My sparql looks like:
SELECT ?label
WHERE
{
wd:Q245068 rdfs:label ?label .
FILTER (langMatches( lang(?label), "EN" ) )
}
When I run this, I get 3 results that are all "comedian".
Why does this return multiple results for the same language/id?
Yes, that's funny...
The reason is obvious if you just check the item
"English" means three different things (here labeled in German, the international language of high comedy): Canadian, British, and American English. You get all three.
Here's how to avoid it:
SELECT DISTINCT ?label
WHERE {
wd:Q245068 rdfs:label ?label .
FILTER (langMatches( lang(?label), "EN" ) )
}
Or, use a more specific language code:
FILTER (langMatches( lang(?label), "EN-GB" ) )
But that runs the risk of not returning any label if it isn't set in the particular variety you've chosen. You can get around that, but really at the end you are just reimplementing the standard service that exists for exactly this purpose:
# just ad "Label" to get the label in the SELECT
SELECT ?item ?itemLabel
WHERE {
?item wet:P31 Q5.
# WITH THIS SERVICE
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
I am looking to retrieve data for a given location.
So using the below I am able to retrieve the bordering locations of France and Scotland.
SELECT (GROUP_CONCAT(?borderLabel;separator=",") AS ?borders)
WHERE {
?location wdt:P47 ?border.
?location wdt:P2046 ?area.
?location wdt:P1082 ?population.
FILTER (?location=wd:Q142 || ?location=wd:Q22)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?border rdfs:label ?borderLabel
}
}
GROUP BY ?location
But as soon as I add anything to the SELECT, ie. SELECT ?locationLabel (GROUP_CONCAT(?borderLabel;separator=",") AS ?borders) it tells me Query is malformed: Bad aggregate
I am however able to add ?location to return the wd entity value without issue.
What is wrong here?
Using the Wikidata SPARQL service, I would like to get the list of the 50 states and include the District of Columbia from Wikidata. I have come up with a kludgy query to do so:
#-- wdt:P31 = instance of; wd:Q35657 = list of states
SELECT ?state ?stateLabel
WHERE {
{?state wdt:P31 wd:Q35657} UNION
{?state wdt:P3403 wd:Q3551781} . #-- coextensive with District of Columbia
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
My query works but the way I extract DC into the results is ugly. (It's possible that future changes to data in Wikidata will break this query.) What I'd like to be able to say is something like
UNION {?state == wd:Q61}
to directly include Washington, D.C. (Q61). However, as a SPARQL newbie, I can't figure out the SPARQL syntax for doing so. I'd be grateful for any help to rewrite this query to directly pull in wd:Q61.
You can use SPARQL 1.1 BIND to add fixed resources to the resultset, i.e.
SELECT ?state ?stateLabel WHERE {
{?state wdt:P31 wd:Q35657}
UNION
{BIND(wd:Q61 as ?state)}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
You could use an external identifier to uniquely identify Washington, D.C., if you're worried about the property that you're currently using being unstable.
For example, to use the Geonames ID for Washington, D.C. in your UNION statement, you could use the following:
# wdt:P31 = instance of; wd:Q35657 = list of states; wdt:P1566 = Geonames ID
SELECT ?state ?stateLabel
WHERE {
{?state wdt:P31 wd:Q35657} UNION
{?state wdt:P1566 "4138106"} . # we want wd:Q61
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}