custom ordering on queries

24 views
Skip to first unread message

Dave S

unread,
Oct 24, 2014, 3:14:44 PM10/24/14
to web...@googlegroups.com
This is a more general DB query, but I've learned to trust the web2py community, and web2py is where the answer will do me the most good.

I'm looking at a table where I want to have a custom ordering on a field.  Standard ascending and descending don't quite work for me in the use I'm interested in.  What I want to do is sort the field ("owner") so that the value I'm most interested in is at the top ("owner='dave'"), and the rest of the entries are sorted asc or desc.  Is there a way to do that in a single query, or do I need to make 2 queries and concatenate the results?  Is "group by" an appropriate SQL clause here?

Also, would that solution generalize to sorting by a set of values of interest at the top ("owner='dave'|'bob'|'andy'")?

Thanks.

/dps

Niphlod

unread,
Oct 24, 2014, 3:34:41 PM10/24/14
to web...@googlegroups.com
<db guy mode>
if the cardinality of "owner" is not high, fetch them all and use "for row in rows.sort(yourownfunction)". If the cardinality is high, either do two queries, or add a "sort_index" integer column that you can use for large sorts.
</db guy mode>

<developer>
use for row in rows.sort()
</developer>

Dave S

unread,
Oct 24, 2014, 6:02:44 PM10/24/14
to web...@googlegroups.com

Sounds good to me!  Both the db guy and the developer have been previously vetted, too!   ;-)

/dps
 
Reply all
Reply to author
Forward
0 new messages