Submit Feedback/Bug Report

myExperiment Logo How To SPARQL

                    Back to Contents Page

7. ORDER BY

The filter on dates example restricts the workflows returned, however you may want to go further, listing the most recent workflows first. The ORDER BY clause can be used to do this:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX mecontrib: <http://rdf.myexperiment.org/ontologies/contributions/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?workflow ?added
WHERE { 
  ?workflow rdf:type mecontrib:Workflow ;
    dcterms:created ?added
  FILTER ( ?added >= xsd:dateTime('2009-09-01T00:00:00Z') ) 
}
ORDER BY DESC(?added)
[Run]
[Hide Example Results]
workflowadded
http://www.myexperiment.org/workflows/10082009-12-22T20:45:54Z
http://www.myexperiment.org/workflows/10052009-12-15T22:33:09Z
http://www.myexperiment.org/workflows/10042009-12-15T22:17:56Z
http://www.myexperiment.org/workflows/10032009-12-15T22:17:11Z
http://www.myexperiment.org/workflows/10022009-12-15T22:16:23Z

The DESC operand means order the results in descending order, in this case, the latest workflows first. If you wanted them ordered ascending you don't require any operand. If you want a second criteria to order on, you just add this after the first. An example of this might be order by workflow type with the latest of each type first:

PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX mecontrib: <http://rdf.myexperiment.org/ontologies/contributions/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?workflow ?added ?ct_title
WHERE { 
  ?workflow rdf:type mecontrib:Workflow ;
    dcterms:created ?added ;
    mebase:has-content-type ?ct .
  ?ct dcterms:title ?ct_title 
  FILTER ( ?added >= xsd:dateTime('2009-09-01T00:00:00Z') ) 
}
ORDER BY ?ct_title DESC(?added)
[Run]
[Hide Example Results]
workflowaddedct_title
http://www.myexperiment.org/workflows/362010-01-12T13:58:46ZTaverna 1
http://www.myexperiment.org/workflows/152009-12-04T16:04:38ZTaverna 1
http://www.myexperiment.org/workflows/9942009-12-04T10:47:04ZTaverna 1
http://www.myexperiment.org/workflows/10052009-12-15T22:33:09ZTaverna 2 beta
http://www.myexperiment.org/workflows/10042009-12-15T22:17:56ZTaverna 2 beta

By default, ORDER BY will treat the field(s) it is ordering on as alphanumeric. In some cases it is necessary to treat a field as being numeric. An example of this is a list of the most downloaded workflows. Below is an example of how to treat ?downloaded as a numeric field.

BASE <http://www.myexperiment.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX mevd: <http://rdf.myexperiment.org/ontologies/viewings_downloads/>
PREFIX sioc: <http://rdfs.org/sioc/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX mecontrib: <http://rdf.myexperiment.org/ontologies/contributions/>

SELECT ?workflow ?downloaded
WHERE {
  ?workflow rdf:type mecontrib:Workflow .
  ?workflow sioc:has_owner <users/43> .
  ?workflow mevd:downloaded ?downloaded
}
ORDER BY DESC(xsd:nonNegativeInteger(?downloaded))
[Run]
[Hide Example Results]

Alphanumeric Ordering

workflowdownloaded
http://www.myexperiment.org/workflows/139299
http://www.myexperiment.org/workflows/12988
http://www.myexperiment.org/workflows/95985
http://www.myexperiment.org/workflows/121598
http://www.myexperiment.org/workflows/110598
http://www.myexperiment.org/workflows/122498
http://www.myexperiment.org/workflows/124698
http://www.myexperiment.org/workflows/131598
http://www.myexperiment.org/workflows/75978
http://www.myexperiment.org/workflows/39976
    

Numeric Ordering

workflowdownloaded
http://www.myexperiment.org/workflows/1407852
http://www.myexperiment.org/workflows/103250
http://www.myexperiment.org/workflows/162918
http://www.myexperiment.org/workflows/722631
http://www.myexperiment.org/workflows/152542
http://www.myexperiment.org/workflows/1242303
http://www.myexperiment.org/workflows/1542162
http://www.myexperiment.org/workflows/1582008
http://www.myexperiment.org/workflows/791788
http://www.myexperiment.org/workflows/191617
                    Back to Contents Page