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