Reputation: 55
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?
Upvotes: 0
Views: 487
Reputation: 8465
Not sure why you want this because ordering is expensive, but here we go:
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...
Extract the number from the entity URI with strafter
function:
extract value after http://www.wikidata.org/entity/Q with
strafter(str(?item), "http://www.wikidata.org/entity/Q")
convert to integer value by using XPath constructor function xsd:integer()
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
Upvotes: 2