TIMEX
TIMEX

Reputation: 271774

In Wikidata SPARQL, how can I return other fields?

In my Wikidata query, I actually have 2 problems.

  1. Date of Birth + Gender + Description is not returned
  2. GROUP BY does not aggregate based on the name (Bad Aggregate Error)
SELECT ?person ?label ?dob ?gender ?description
WHERE
{
  ?person wdt:P31 wd:Q5;
          rdfs:label ?label.
  FILTER(STRSTARTS(?label, "Michael Bloom")).
}
GROUP BY ?person ?label
LIMIT 5

Try it here

Upvotes: 0

Views: 359

Answers (1)

Querybook
Querybook

Reputation: 635

  1. You must query all fields for them to being returned (SPARQL can't guess where to get them), with the exception of labels and descriptions when using Wikibase Label Service.
  2. All fields in your SELECT must be in the GROUP BY clause, with the exception of the use of the GROUP_CONCAT function that will concatenate all values into one field.
  3. Bad news: searching humans by other than their exact label in Wikidata Query Service is hard, as you will often hit the timeout of 60 seconds. A solution, as shown by AKSW, is to use Wikibase Mediawiki API and do an entity search.

Here is an example of your query rewritten:

SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?dob ; SEPARATOR = ' / ') AS ?dob) (GROUP_CONCAT(DISTINCT ?gender ; SEPARATOR = ' / ') AS ?gender)
WHERE {
  SERVICE wikibase:mwapi {
    bd:serviceParam wikibase:api "EntitySearch" .
    bd:serviceParam wikibase:endpoint "www.wikidata.org" .
    bd:serviceParam mwapi:search "Michael Bloom" .
    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 }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
GROUP BY ?person ?personLabel ?personDescription

Upvotes: 2

Related Questions