Page 1 of 1

Query Builder - How to sort by a column alias (aggregate)

PostPosted: Mon Jan 14, 2019 6:58 pm
by huber
Hi

First of all a happy new year to you all, wishing good health, happiness, and many successful Servoy projects :D

I have the following SQL code and would like to rewrite it as QB Select. But I didn't find an example, docu or hint on how a QB sort is done by an alias like needed in the following SELECT.
Code: Select all
SELECT supplier, MAX(creation_date) as theDate
FROM invoices
GROUP BY supplier
ORDER BY theDate DESC;


The missing piece is how to write the query.sort
Code: Select all
var query = datasources.db.<name>.invoices.createSelect();
var i = query.columns;
query.result
   .add(i.supplier)
   .add(i.creation_date.max, 'creationDate');
query.groupBy
   .add(i.supplier);
query.sort
   .add(???);


Best regards,

Re: Query Builder - How to sort by a column alias (aggregate

PostPosted: Tue Jan 22, 2019 2:46 pm
by jcompagner
the docs should be quite clear what you can put in there:

query.sort.add(query.columns.orderid)

or

query.sort.add(query.columns.orderid.desc)

Re: Query Builder - How to sort by a column alias (aggregate

PostPosted: Wed Jan 23, 2019 12:43 pm
by huber
Ah, ok, a column with a method is a valid sort column, like .add(query.columns.creation_date.max.desc)

Regards,