Bad aggregate in Sparql request for Wikidata - sparql

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

Related

Get birthplace and date of American investors from DBpedia people in SPARQL

I'm trying to get a list of Investors from the US, their birth dates, and places.
But the SPARQL code below doesn't seem to work. I tried it on both
DBpedia pages:
https://live.dbpedia.org/sparql or
https://dbpedia.org/snorql/
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?name ?birthPlace ?birthDate
WHERE {
?person a dbo:BusinessPerson, dbo:Entrepreneur, dbo:Investor .
?person dbo:birthPlace ?birthPlace .
?person dbo:birthDate ?birthDate .
?person dbo:country dbr:United_States .
?person rdfs:label ?name .
FILTER(LANG(?name) = 'en')
}
Can't find any good literature on this subject either so having trouble understanding what I should alter.
All advice is appreciated.
Try the following query here:
SELECT distinct ?person ?label ?birthPlace ?birthDate
WHERE {
{
?person rdf:type dbo:Person.
?person ?p dbr:Investor.
?person dbo:nationality dbr:United_States.
?person rdfs:label ?label. FILTER (lang(?label) = 'en')
OPTIONAL{?person dbp:birthPlace ?birthPlace.}
OPTIONAL{?person dbo:birthDate ?birthDate }
}
Union
{
?person rdf:type dbo:Person.
?person ?p dbo:Entrepreneur.
?person dbo:nationality dbr:United_States.
?person rdfs:label ?label. FILTER (lang(?label) = 'en')
OPTIONAL{?person dbp:birthPlace ?birthPlace.}
OPTIONAL{?person dbo:birthDate ?birthDate }
}
Union
{
?person rdf:type dbo:Person.
?person a dbo:BusinessPerson.
?person dbo:nationality dbr:United_States.
?person rdfs:label ?label. FILTER (lang(?label) = 'en')
OPTIONAL{?person dbp:birthPlace ?birthPlace.}
OPTIONAL{?person dbo:birthDate ?birthDate }
}
}
you can also try this equivalent query on wikidata SPARQL endpoint here:
SELECT ?person ?personLabel ?dob ?pobLabel
WHERE
{
?person wdt:P31 wd:Q5.
?person wdt:P106 wd:Q557880.
?person wdt:P27 wd:Q30.
OPTIONAL {?person wdt:P569 ?dob.}
OPTIONAL {?person wdt:P19 ?pob. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Query timeout all living people on WikiData

I have the following query to fetch all living people on wikidata but it's timing out. Any suggestions on how to optimize it or change it to make it run?
SELECT ?person ?personLabel WHERE {
?person wdt:P31 wd:Q5.
OPTIONAL { ?person wdt:P570 ?dateOfDeath }
FILTER(!BOUND(?dateOfDeath))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Wikidata query throwing StackOverflowError after update

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

Get alias values from Wikidata for a given property?

For a given property like 'occupation (P106)', I want to retrieve all its aliases like: profession, job, work, career, employment, craft. All of this is present on the properties wikidata page, under 'Also known as'. How can I go about retrieving this using SPARQL?
I tried using the following query.
SELECT ?predicate ?object WHERE {
wdt:P106 wdt:P1449 ?predicate . //Nickname
wdt:P106 wdt:P734 ?predicate . //Family Name
wdt:P106 wdt:P735 ?predicate . //Given Name
wdt:P106 skos:altLabel ?predicate .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?altLabel
{
VALUES (?wd) {(wd:P106)}
?wd skos:altLabel ?altLabel .
FILTER (lang(?altLabel) = "en")
}
or
SELECT ?altLabel
{
VALUES (?wdt) {(wdt:P106)}
?wd wikibase:directClaim ?wdt .
?wd skos:altLabel ?altLabel .
FILTER (lang(?altLabel) = "en")
}
These paragraphs provide some explanation:
Truthy statements
Properties
Predicates
Update
You still could use the label service:
SELECT ?wdAltLabel
{
VALUES (?wdt) {(wdt:P106)}
?wd wikibase:directClaim ?wdt .
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". }
}