Search code examples
sparqlrdf

SPARQL, get the next value in a result set based on a total order function


Consider a SPARQL query q1 with a single head variable ?y, i.e. q1(?y) :- some pattern that has a result set res(q1).

Problem:

Given a concrete value vi in res(q1), I want to determine the next value vi+1 in res(q1) depending on a specific total order function (for example < for integer values).

Example:

Consider posing the following query against DBpedia SPARQL endpoint.

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

select ?y where {

 ?x <http://dbpedia.org/ontology/wikiPageID> ?y

}
order by ?y
LIMIT 10

This will result in

these results

Now given the value vi = 10, then vi+1 = 12, as can be seen from the results.

The only way in my mind to do this is using external programming snippet; that gets a list of ordered SPARQL query results (usingORDER BY), goes to vi, then retrieves the next value which should be vi+1.

QUESTION:

Is there any workaround that could express this in SPARQL without the need for coding, (no problem if vi is a fixed value in the query)?


Solution

  • If it's okay to assume that you know vi a priori, you could use a filter and a limit to only retrieve vi+1

    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    
    select ?y where {
    
     ?x <http://dbpedia.org/ontology/wikiPageID> ?y
     filter(?y > 10)
    
    }
    order by ?y
    LIMIT 1
    

    The query above yields a single value, 12