0

I have the following query:

SELECT ?admin ?name ?abbr
WHERE {
  VALUES ?admin { wd:Q771 }
  ?admin wdt:P131 ?country;
         wdt:P17 ?country.
  OPTIONAL { ?admin wdt:P1813 ?alias. }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?admin rdfs:label ?name.
    ?alias rdfs:label ?abbr.
  }
}

This retrieves the administration in VALUES with its English name and abbrevations, if available. For example:

admin name abbr
wd:Q771 Massachusetts MA
wd:Q771 Massachusetts Mass.

Now I would like to filter it such that only the shortest abbreviation is displayed. For the above output, for example, I would only get one record, the one with MA. How can I accomplish this?

theberzi
  • 2,142
  • 3
  • 20
  • 34
  • 2
    `SELECT ?admin ?name ?abbr WHERE { VALUES ?admin { wd:Q771 } ?admin wdt:P131 ?country; wdt:P17 ?country. OPTIONAL { ?admin wdt:P1813 ?abbr. } SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?admin rdfs:label ?name. } filter not exists { ?admin wdt:P1813 ?abbrOther filter(?abbr != ?abbrOther && strlen(str(?abbrOther)) < strlen(str(?abbr))) } }` – UninformedUser May 10 '21 at 09:12
  • Thank you, this seems to work nicely. If you add it as an answer I will mark it as accepted. – theberzi May 10 '21 at 09:42

0 Answers0