Limit Wikidata results to only one label where more are available

149 Views Asked by At

I Have the following SPARQL query for Wikidata:

SELECT DISTINCT ?location ?place_eng ?admin_eng ?country_eng WHERE {
  {
    VALUES ?place { wd:Q23154 wd:Q19319 wd:Q2758 }
    OPTIONAL { ?place wdt:P625 ?location. }
    ?place wdt:P131 ?admin.
    ?place wdt:P17 ?country.
  }

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?place rdfs:label ?place_eng.
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?admin rdfs:label ?admin_eng.
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?country rdfs:label ?country_eng.
  }
}

Which selects three specific cities with their location and labels for name, administration name and country name. If you run the query in Wikidata Query service you can see that one of the cities appears twice, because there are two different labels available for its administration.

How can I make sure each city appears only once? In case there are multiple labels, I only care about getting one (the first one, or even at random, provided it's the same every time I run the query).

I've already tried this solution from a similar question (only the first line changes):

SELECT DISTINCT ?location ?place_eng (group_concat(distinct ?admin_eng) as ?admin_eng) ?country_eng WHERE {
  {
    VALUES ?place { wd:Q23154 wd:Q19319 wd:Q2758 }
    OPTIONAL { ?place wdt:P625 ?location. }
    ?place wdt:P131 ?admin.
    ?place wdt:P17 ?country.
  }

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?place rdfs:label ?place_eng.
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?admin rdfs:label ?admin_eng.
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?country rdfs:label ?country_eng.
  }
}

But I get a Bad Aggregate error.

0

There are 0 best solutions below