I would like to create a ranking of observations using SPARQL. Suppose I have:
@prefix : <http://example.org#> .
:A :value 60 .
:B :value 23 .
:C :value 89 .
:D :value 34 .
The ranking should be: :C = 1 (the highest), :A = 2, :D = 3, :B = 4. Up until now, I was able solve it using the following query:
prefix : <http://example.org#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?x ?v ?ranking {
?x :value ?v .
{ SELECT (GROUP_CONCAT(?x;separator="") as ?ordered) {
{ SELECT ?x {
?x :value ?v .
} ORDER BY DESC(?v)
}
}
}
BIND (str(?x) as ?xName)
BIND (strbefore(?ordered,?xName) as ?before)
BIND ((strlen(?before) / strlen(?xName)) + 1 as ?ranking)
} ORDER BY ?ranking
But that query only works if the URIs for ?x
have the same length. A better solution would be to have a function similar to strpos
in PHP or isIndexOf
in Java, but as far as I know, they are not available in SPARQL 1.1. Are there simpler solutions?