order by the greater of 2 fields

55 views
Skip to first unread message

Anthony Smith

unread,
Oct 24, 2016, 7:11:29 AM10/24/16
to web2py-users
Hi all,

I have table with 2 fields, withhold _until_date and esi_withhold_until.

Is is it possible to order by so the the greater of the 2 fields was at the top of the list.

cheers 

Anthony  

黄祥

unread,
Oct 24, 2016, 8:55:23 AM10/24/16
to web2py-users
pls try (not tested)
myorder = db.mytable.withhold_until_date | db.mytable.esi_withhold_until
db().select(db.mytable.ALL, orderby = myorder)

ref:

best regards,
stifan

Anthony

unread,
Oct 24, 2016, 10:10:12 AM10/24/16
to web2py-users
On Monday, October 24, 2016 at 8:55:23 AM UTC-4, 黄祥 wrote:
pls try (not tested)
myorder = db.mytable.withhold_until_date | db.mytable.esi_withhold_until
db().select(db.mytable.ALL, orderby = myorder)

That will order by withhold_until_date, with esi_withhold_until breaking any ties -- it will not order by the max of those two fields.

Anthony

Anthony

unread,
Oct 24, 2016, 10:16:19 AM10/24/16
to web2py-users
On Monday, October 24, 2016 at 7:11:29 AM UTC-4, Anthony Smith wrote:
Hi all,

I have table with 2 fields, withhold _until_date and esi_withhold_until.

Is is it possible to order by so the the greater of the 2 fields was at the top of the list.

I think there are various ways to achieve this in SQL, though not sure you could easily use the DAL to generate that SQL. Does you query involve a limitby or pagination (i.e., does the sorting have to be done by the database as part of the query)? If not, you could instead do the sorting in Python -- check out the Rows.sort method: http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#find--exclude--sort.

Anthony

Anthony Smith

unread,
Oct 25, 2016, 6:56:42 AM10/25/16
to web2py-users
Hi all, 
This gets the results using sql but only able to do one or the other is the controller in web2py
select * from animalTask order by GREATEST(COALESCE(withhold_until_date,0),COALESCE(esi_withhold_until_date,0)) DESC 

Anthony

Dave S

unread,
Oct 25, 2016, 2:11:45 PM10/25/16
to web2py-users


On Tuesday, October 25, 2016 at 3:56:42 AM UTC-7, Anthony Smith wrote:

Hi all, 
This gets the results using sql but only able to do one or the other is the controller in web2py
select * from animalTask order by GREATEST(COALESCE(withhold_until_date,0),COALESCE(esi_withhold_until_date,0)) DESC 

Anthony

Anthony

unread,
Oct 26, 2016, 12:25:35 AM10/26/16
to web2py-users
Hi all, 
This gets the results using sql but only able to do one or the other is the controller in web2py
select * from animalTask order by GREATEST(COALESCE(withhold_until_date,0),COALESCE(esi_withhold_until_date,0)) DESC 

What do you mean by "one or the other" -- what is "the other"?

Anthony

Anthony Smith

unread,
Oct 26, 2016, 6:15:54 AM10/26/16
to web2py-users
I meant it would only order by 1 field not the other,but the sql works fine.
just implementing it now  

I will work on it 

cheers
Reply all
Reply to author
Forward
0 new messages