How do I make a SPARQL query to find the highest value for a property?

后端 未结 2 668
青春惊慌失措
青春惊慌失措 2021-01-18 12:48

Lets say I have a predicate like \'age\' where the values of all age triples are integer literals. What SPARQL query would return the subject with the highest age in the dat

2条回答
  •  一整个雨季
    2021-01-18 13:11

    You just need to do order by desc with the age predicate and then limit to just get the first one.

    PREFIX ns:    
    SELECT ?s ?age
    WHERE { ?s ns:age ?age }
    ORDER BY DESC(?age) LIMIT 1
    

    See the semantics of order by in SPARQL here

    With the next version of SPARQL , 1.1, that is already supported in some systems you can use function aggregates and do ..

    SELECT (max(?age) as ?maxage)
    WHERE { ?s ns:age ?age }
    

    This is not supported in all triple stores currently.

提交回复
热议问题