Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting SPARQL results by date

Is there a way to sort the results by creation date?

Example query which must be sortet:

SELECT * WHERE {?s ?p ?o} limit 200
like image 280
cupakob Avatar asked Jul 20 '09 16:07

cupakob


People also ask

How do I sort by date in SPARQL?

You can use an order by clause just like you can in SQL. Note that per the SPARQL specification, the ORDER BY only applies to SELECT queries. You will need to create a binding for the property you want to use for ordering.

What is FOAF in SPARQL?

Dataset: Friend of a Friend (FOAF) @prefix card: <http://www.w3.org/People/Berners-Lee/card#> .

How do SPARQL queries work?

SPARQL from Within Correspondingly, a SPARQL query consists of a set of triple patterns in which each element (the subject, predicate and object) can be a variable (wildcard). Solutions to the variables are then found by matching the patterns in the query to triples in the dataset.

What does SPARQL stand for?

SPARQL is a recursive acronym, which stands for SPARQL Protocol and RDF Query Language. SPARQL consists of two parts: query language and protocol. The query part of that is pretty straightforward. SQL is used to query relational data. XQuery is used to query XML data.


2 Answers

You can use an order by clause just like you can in SQL. Note that per the SPARQL specification, the ORDER BY only applies to SELECT queries. You will need to create a binding for the property you want to use for ordering.

SELECT ?s ?p ?o
WHERE {?s ?p ?o .
       ?s <creationDatePredicate> ?date . }
ORDER BY DESC(?date)
LIMIT 200

Update: If you're not already storing the creation date, you will need to store the metadata yourself. As to how do to do that, you have a few options:

  1. Store a triple in your default graph which has the creation date for each subject. If you're storing more than one graph, this will become unwieldy very quickly and probably is not what you want.

  2. Store each graph as a named graph in your RDF triple store. You can then store metadata about each graph in the default graph or in one "shared" named graph for creation times.

  3. Store your data in the named graph and use a named graph to store the creation time metadata.

An example of option #2's SPARQL query would be:

SELECT ?s ?p ?o
WHERE {
        GRAPH ?g { ?s ?p ?o . }
        ?g <creationDatePredicate> ?date . }
ORDER BY DESC(?date)
LIMIT 200
like image 169
Phil M Avatar answered Oct 21 '22 23:10

Phil M


Since you've said you don't actually store the creation date in your RDF then any possible mechanism for doing this would be specific to the SPARQL implementation you were using and the backing RDF store or whatever. It's quite likely that it's just not possible.

like image 4
tialaramex Avatar answered Oct 21 '22 22:10

tialaramex