Thread: Custom sort
Can you define a custom sort in postgres? For instance in mysql, you could do something like (I forget the exact syntax) ORDER BY FIND_IN_SET(column_name, ('one','two','three')) Art
Artacus a écrit : > Can you define a custom sort in postgres? For instance in mysql, you > could do something like (I forget the exact syntax) > > ORDER BY FIND_IN_SET(column_name, ('one','two','three')) > I don't really know this syntax but isn't it something like : ORDER BY column_name='one', column_name='two', column='three' ? -- Guillaume. http://www.postgresqlfr.org http://dalibo.com
In response to Artacus <artacus@comcast.net>: > Can you define a custom sort in postgres? For instance in mysql, you > could do something like (I forget the exact syntax) > > ORDER BY FIND_IN_SET(column_name, ('one','two','three')) You could do this by defining an ENUM for the values. ENUMs sort in the order the values were defined. You could also define a pgsql function that did the sort for you, if ENUMs didn't cut it. i.e.: SELECT sort_col FROM some_table ORDER BY my_sort_func(sort_col); -- Bill Moran Collaborative Fusion Inc. http://people.collaborativefusion.com/~wmoran/ wmoran@collaborativefusion.com Phone: 412-422-3463x4023 **************************************************************** IMPORTANT: This message contains confidential information and is intended only for the individual named. If the reader of this message is not an intended recipient (or the individual responsible for the delivery of this message to an intended recipient), please be advised that any re-use, dissemination, distribution or copying of this message is prohibited. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. ****************************************************************
On Thu, 2008-08-14 at 10:22 -0700, Artacus wrote: > Can you define a custom sort in postgres? For instance in mysql, you > could do something like (I forget the exact syntax) > > ORDER BY FIND_IN_SET(column_name, ('one','two','three')) > You can sort by any column, or arbitrary expression or function. Regards, Jeff Davis
Artacus wrote: > Can you define a custom sort in postgres? For instance in mysql, you > could do something like (I forget the exact syntax) > > ORDER BY FIND_IN_SET(column_name, ('one','two','three')) The simplest direct mapping would probably be a CASE statement (see the PostgreSQL documentation). As others have noted, enums and Pl/PgSQL functions are also options. -- Craig Ringer
>> Can you define a custom sort in postgres? For instance in mysql, you >> could do something like (I forget the exact syntax) >> >> ORDER BY FIND_IN_SET(column_name, ('one','two','three')) >> > > I don't really know this syntax but isn't it something like : > ORDER BY column_name='one', column_name='two', column='three' ? Thanks all. I'm not sure it's the best solution but, here's what I ended up doing ORDER BY strpos(column_name, 'One,Two,Three')