I will use generic terms for classes here.
I have a set of Objects, each of those has multiple Properties linking them to Subjects. Subject has a integer value associated with it through another property.
For each Object I want to retrieve its Subject with maximum value.
Retrieving a Subject with maximum value for one Object is easy, based on this: How do I make a SPARQL query to find the highest value for a property? . Since I want the Subject itself (and not only its value), I used the solution of "get all subjects, order them, LIMIT 1".
However, I cannot get this approach working for multiple Objects. I tried:
SELECT ?object ?subject ?subMaxVal
WHERE {
?object a :Object.
{
SELECT ?subject (?value as ?subMaxVal)
WHERE {
?object :Predicate ?subject.
?subject :value ?value.
}
ORDER BY DESC(?value)
LIMIT 1
}
}
This returns incorrect results though. It finds a value for ?subMaxVal
, but then returns this value with all Objects in the dataset (even though this value is only correct for one of them).
I found a mention saying that SPARQL sub-queries are executed inner-first. I cannot wrap my head around how to form the query to perform the sub-query for each object though.