Search code examples
pythondatabaseopen-sourcedatajoint

What is the correct ORDER BY syntax in DataJoint for Python?


In DataJoint for Python, what is the correct syntax of the ORDER BY argument used in the fetch command?

The current documentation (as of 2018-08-17) only specifies the following syntax for DataJoint in MATLAB:

s = fetch(experiment.Session, '*', 'ORDER BY session_date DESC LIMIT 5')

Solution

  • In Python, the correct syntax for the ORDER BY argument is

    table.fetch(order_by='attr')
    

    If you need to sort by multiple attributes, provide them as a tuple:

    table.fetch(order_by=('attr1', 'attr2'))
    

    This will sort the fetched items in ascending order with respect to the specified attributes. To make it descending, add the word DESC to the attributes.

    table.fetch(order_by='attr DESC')
    table.fetch(order_by=('attr1 DESC', 'attr2'))
    

    You may optionally also use the word ASC to make the ascending order implicit but omitting it has the same effect.