Wikidata query throwing StackOverflowError after update - sql

Before, my query worked perfectly. However, something changed on their backend, and now I'm getting an error when running this query.
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?dob ; SEPARATOR = ' | ') AS ?dob) (GROUP_CONCAT(DISTINCT ?gender ; SEPARATOR = ' | ') AS ?gender) (GROUP_CONCAT(DISTINCT ?image ; SEPARATOR = ' | ') AS ?image)
WHERE {
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api "EntitySearch" .
bd:serviceParam wikibase:endpoint "www.wikidata.org" .
bd:serviceParam mwapi:search "william" .
bd:serviceParam mwapi:language "en" .
?person wikibase:apiOutputItem mwapi:item .
}
?person wdt:P31 wd:Q5 .
OPTIONAL { ?person wdt:P569 ?dob }
OPTIONAL { ?person wdt:P21 ?gender }
OPTIONAL { ?person wdt:P18 ?image }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
GROUP BY ?person ?personLabel ?personDescription

Related

Bad aggregate in Sparql request for Wikidata

I try to get all the (famous because referenced in wikidata) people alive and their occupation (concatenated), but the query engine query editor disclaim a "syntax" problem that i can't debugg. Could you help me to re-arrange the request to make it work?
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
?person wdt:P106 ?occupation.
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person
To fix this, you can add all your non-aggregated variables to the GROUP BY clause:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
?person wdt:P106 ?occupation.
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person ?personLabel ?personDescription
However, I wasn't able to get that to run without timing out. Using a query hint to turn off the query optimizer and moving this around allowed the query to run:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
hint:Query hint:optimizer "None".
{
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
}
?person wdt:P106 ?occupation.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person ?personLabel ?personDescription
At this point, we can see that there isn't any data for the ?positions variable.
That suggests to me that Wikidata's magical population of the Label variables (e.g. ?occupationLabel) might not be working with the aggregation.
Pulling the aggregation out into a new top-level query, and doing the matching in a subquery allows the variables to be assigned correctly, but I still had trouble getting this to not timeout:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions) WHERE {
SELECT ?person ?personLabel ?personDescription ?occupationLabel
WHERE {
hint:Query hint:optimizer "None".
{
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
}
?person wdt:P106 ?occupation.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
}
GROUP BY ?person ?personLabel ?personDescription
Some careful optimization (or execution in parts) may be required to get this to fully execute. For example, limiting to 1000 (non-deterministic) person-occupation pairs will run without timing out:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions) WHERE {
SELECT ?person ?personLabel ?personDescription ?occupationLabel
WHERE {
hint:Query hint:optimizer "None".
{
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
}
?person wdt:P106 ?occupation.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
LIMIT 1000
}
GROUP BY ?person ?personLabel ?personDescription

How do I create a Wikidata SPARQL query to return the top 10 US companies and their directors based on revenue?

Here is the query I am working with:
SELECT DISTINCT ?item ?itemLabel ?rev ?date (group_concat(distinct ?directorLabel ; separator = ", ") as ?directors)
WHERE {
?item wdt:P31/wdt:P279* wd:Q4830453 ;
wdt:P17 wd:Q30 .
?item p:P2139 ?revSt .
?revSt pq:P585 ?date.
FILTER ( YEAR(?date) = 2020 ).
?revSt ps:P2139 ?rev .
?item p:P3320 ?relationship.
?relationship ps:P3320 ?director.
#Filter Director by no end date
FILTER NOT EXISTS {?relationship pq:P582 ?end.}
#Group directors
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?director rdfs:label ?directorLabel .
?item rdfs:label ?itemLabel .
}
}
GROUP BY ?item ?itemLabel
ORDER BY DESC(?rev)
LIMIT 10
This query is a bad aggregate, if I add more variables to the GROUP BY clause it times out. Ultimately I would like to aggregate the wdt ids for all the directors too.
SELECT DISTINCT
?item ?itemLabel ?rev ?date
(group_concat(distinct ?directorLabel ; separator = ", ") as ?directors)
WHERE {
?item wdt:P31/wdt:P279* wd:Q4830453 ;
wdt:P17 wd:Q30 .
?item p:P2139 ?revSt .
?revSt pq:P585 ?date.
hint:Prior hint:rangeSafe "true" .
FILTER ( YEAR(?date) = 2020 ).
?revSt ps:P2139 ?rev .
?item p:P3320 ?relationship.
?relationship ps:P3320 ?director.
FILTER NOT EXISTS {?relationship pq:P582 ?end.}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?director rdfs:label ?directorLabel .
?item rdfs:label ?itemLabel .
}
}
GROUP BY ?item ?itemLabel ?rev ?date
ORDER BY DESC(?rev)
LIMIT 10

SPARQL Query: A sets of breeds cat along with the cats that belong to

I am trying to query all the cats breeds (wd:Q43577) along with the list of cats that belong to. Below, you will find my solution. The result of my solution is not relevant and contains a redundancy. Therefore, I need your help to find the best solution. (to execute this query, you have to use this link).
SELECT ?CatsBreedsID ?CatsBreedsName ?CategoryID ?CategoryName ?CatsID ?CatsName where {
?CatsBreedsID wdt:P31 wd:Q43577.
OPTIONAL{
?CatsBreedsID wdt:P279 ?CategoryID.
?CatsID wdt:P31 ?CategoryID.
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?CatsBreedsID rdfs:label ?CatsBreedsName .
?CategoryID rdfs:label ?CategoryName .
?CatsID rdfs:label ?CatsName.
}
}
You should use the GROUP_CONCAT function if I understand your question correctly.
Try a query like this:
SELECT ?CatsBreedsID ?CatsBreedsName ?CategoryID ?CategoryName
(GROUP_CONCAT(DISTINCT ?CatsID; SEPARATOR=', ') AS ?CatsID_List)
(GROUP_CONCAT(DISTINCT ?CatsName; SEPARATOR=', ') AS ?CatsName_List)
WHERE {
?CatsBreedsID wdt:P31 wd:Q43577.
OPTIONAL{
?CatsBreedsID wdt:P279 ?CategoryID.
?CatsID wdt:P31 ?CategoryID.
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?CatsBreedsID rdfs:label ?CatsBreedsName .
?CategoryID rdfs:label ?CategoryName .
?CatsID rdfs:label ?CatsName.
}
} GROUP BY ?CatsBreedsID ?CatsBreedsName ?CategoryID ?CategoryName

Wikidata SPARQL query cast & crew

My objective is to create a sparql query that retrieves a list of cast and crew and it's wikidata type ("director", "screenwriter", "cast member", ...)
So far I have this:
SELECT ?titleLabel ?castLabel ?property ?propertyLabel
WHERE {
?title wdt:P345 "tt0848228".
?title wdt:P57 ?cast.
?property wikibase:propertyType ?propertyType.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
The output I want to achive is a table with rows like:
"The Avengers", "Joss Whedon", "wd:P57", "director"
...
I came up with this query:
SELECT ?titleLabel ?castLabel ?property ?propLabel
WHERE {
?title wdt:P345 "tt0848228".
# take all claims on this movie
?title ?property ?cast .
# that involve a human
?cast wdt:P31 wd:Q5 .
# get the property label
# see https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries#Adding_labels_for_properties
hint:Query hint:optimizer "None" .
?prop wikibase:directClaim ?property .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

How to retrieve aliases from wikidata

I'm trying to retrieve some information from Wikidata and I have found interesting to collect the aliases of the voices. For examples Francesco Totti is also known as il Capitano or er Pupone :
I'm trying to retrieve all the serie a's football players with this sparql query:
SELECT ?subject ?nomeLabel ?cognomeLabel ?subjectLabel WHERE {
?subject wdt:P31 wd:Q5.
?subject p:P54 ?team .
?team ps:P54 wd:""" + team_code +""" .
FILTER NOT EXISTS { ?team pq:P582 ?end
}
OPTIONAL{
?subject wdt:P735 ?nome .
?subject wdt:P734 ?cognome .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "it". }
}
ORDER BY (?cognomeLabel)
How I can modify my query to take also the aliases?
Thanks
I have attempted a query with various labels. Here just for Roma:
SELECT distinct ?subject ?subjectLabel ?nomeLabel ?cognomeLabel ?nickname ?alternative ?subjectAltLabel WHERE {
?subject wdt:P31 wd:Q5.
?subject p:P54 ?team .
?team ps:P54 wd:Q2739 .
FILTER NOT EXISTS { ?team pq:P582 ?end . }
OPTIONAL { ?subject wdt:P735 ?nome . }
OPTIONAL { ?subject wdt:P734 ?cognome . }
OPTIONAL { ?subject wdt:P1449 ?nickname . }
OPTIONAL { ?subject skos:altLabel ?alternative . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en,fr". }
}
ORDER BY (?cognomeLabel)
I believe the P1449 property should be the most appropriate property to store an alias/nickname, but it does not seem to be used that much for football players. I just added "il Capitano" to Francesco Totti. Beyond that one there does not seem to be other nicknames for Roma players.
The "Also known as" label (in the right column) is not necessarily the nickname, but may be a spelling variation.
Something more generic if someone is interested in all properties that will return only the english also known as:
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?property a wikibase:Property .
OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
GROUP BY ?property ?propertyLabel ?propertyDescription
LIMIT 5000
Another more simple example for male actors with itemAltLabel :
#Male Actors
SELECT ?item ?itemLabel ?itemAltLabel
WHERE
{
?item wdt:P21 wd:Q6581097.
?item wdt:P106 wd:Q33999.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}