Search code examples
databasesparqlrdf

SPARQL - How to get only newest entities


In my triple store i've a collection of schema:CreativeWork which has the property schema:version and schema:dateCreated. Now i want to get all schema:CreativeWork but only the newest ones. My sample:

PREFIX schema: <https://schema.org/> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT *
WHERE { 
    ?subject rdf:type schema:CreativeWork .     
    ?subject schema:identifier ?identifier .
    ?subject schema:version ?version .
    ?subject schema:dateCreated ?dateCreated .
    OPTIONAL {?subject schema:about/schema:name ?name .}
    FILTER( ?identifier = "46d8b7abfec44865a567ea04e385661b" ) .
} LIMIT 10

How do i manage to query only the latest version?

executable sample: https://api.triplydb.com/s/rLq4V-JgS

Note: FILTER( ?identifier = "46d8b7abfec44865a567ea04e385661b" ) . is just to make it easier.


Solution

  • The query of UninformedUser is working well:

    PREFIX schema: <https://schema.org/> 
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    SELECT DISTINCT * 
    {
      {
        SELECT ?identifier (max(?dateCreated) as ?latestDate) 
        WHERE {     
          ?subject rdf:type schema:CreativeWork .       
          ?subject schema:identifier ?identifier .     
          ?subject schema:dateCreated ?dateCreated . 
        } group by ?identifier
      }     
      ?subject schema:identifier ?identifier .     
      ?subject schema:version ?version .    
      ?subject schema:dateCreated ?latestDate .     
      OPTIONAL {?subject schema:about/schema:name ?name . } 
    } LIMIT 100