0

I expect to retrieve 10 citizens of Canada, whereas the result set ought to start at the lowest wikidata ID Q... it matches (i.e. the search should "start" at https://www.wikidata.org/wiki/Q1) :

    SELECT DISTINCT ?item ?itemLabel 
    WHERE { 
    ?item wdt:P31 wd:Q5 . 
    ?item wdt:P27 wd:Q16 . 
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
    } 
    LIMIT 10 OFFSET 0

But the curent result seems to provide arbitrary findings, e.g. https://www.wikidata.org/wiki/Q116544 (= ice hockey player Danny Gare)

I have not manually checked any entries for canadian citizens with lower WIKIDATA Q id's than Q116544, but I assume that there are some / many.

What do I have to add to get the expected results?

MarkHelms
  • 55
  • 10

1 Answers1

2

Not sure why you want this because ordering is expensive, but here we go:

First try

Simply using ORDER BY on the ?item:

SELECT DISTINCT ?item ?itemLabel WHERE { 
  ?item wdt:P31 wd:Q5 . 
  ?item wdt:P27 wd:Q16 . 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
} 
ORDER BY ASC(?item)
LIMIT 10 OFFSET 0

The drawback, sorting is done lexicographically...

Workaround

Extract the number from the entity URI with strafter function:

  1. extract value after http://www.wikidata.org/entity/Q with

    strafter(str(?item), "http://www.wikidata.org/entity/Q")

  2. convert to integer value by using XPath constructor function xsd:integer()

  3. BIND to variable

Final Query:

SELECT DISTINCT ?item ?itemLabel WHERE { 
   ?item wdt:P31 wd:Q5 . 
   ?item wdt:P27 wd:Q16 . 
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
   BIND(xsd:integer(strafter(str(?item), "http://www.wikidata.org/entity/Q")) as ?number)
} 
ORDER BY ASC(?number)
LIMIT 10 OFFSET 0
Community
  • 1
  • 1
UninformedUser
  • 8,397
  • 1
  • 14
  • 23
  • Thanks! First try provided as first result wd:Q206, but then, after a huge gap, wd:Q1000439. I would assume that there are results in between, too. The workaround seems to be fine, though it is a costly query (~ 20s). – MarkHelms Jun 26 '17 at 10:16
  • As I said, the first query sorts based on strings rather than integer values, i.e. `1 10 11 .. 19 2 20 21 ` And of course, sorting is expensive. – UninformedUser Jun 26 '17 at 11:28