sql - Sorting NULL values in PostgreSQL -


this question has answer here:

in postgresql, i'm using order by on columns of various types (numeric, text, boolean, text[]). i'm designing ajax gui users may interactively sort records individual columns (in both ascending , descending order, using familiar ▲▼ symbols in table heading).

the problem rows contain null values. generally, there few such rows (100/10000) , contain erroneous data i'm not willing hide, not show much. want null values fall @ end of list when using order by (user see them navigates last page in pagination), no matter whether order descending or ascending.

for numeric attributes, example, found out null values seem considered greatest, , hence appear @ top of list when sorting descendingly. that's don't want.

i came 1 ugly hack, i'm ashamed show, work (in python):

"coalesce(value, '%sinfinity')" % ('-' if sort_order == 'descending' else '') 

i keep ugly guy in code if general enough. it's not. works integers, doubles , timestamps, not texts, booleans etc. because there no (at least haven't found any) maximal string constant greater other strings, such infinity greatest of numbers. indeed, put 'zzzzz' code, extremely unhygienic decided ask here @ :)

is there other, elegant way of doing want?

use:

select ... ... order the_column nulls last; 

look @ documentation.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -