sql - PostgreSQL: order by column, with specific NON-NULL value LAST -
when discovered nulls last, kinda hoped generalised 'x last' in case statement in order by portion of query.
not so, seem.
i'm trying sort table 2 columns (easy), output in specific order (easy), one specific value of 1 column appear last (got done... ugly).
let's columns zone , status (don't blame me naming column zone - didn't name them). status takes 2 values ('u' , 's'), whereas zone can take of 100 values.
one subset of zone's values (in pseudo-regexp) in[0-7]z, , first in result. that's easy case.
zone can take value 'future', should appear last in result.
in typical kludgy-munge way, have imposed case value of 1000 follows:
group zone, status order ( case when zone='in1z' 1 when zone='in2z' 2 when zone='in3z' 3 . . -- other in[x]z etc . when zone = 'future' 1000 else 11 -- [number of defined cases +1] end), zone, status this works, it's kludge, , wonder if there might one-liner doing same.
there cleaner way achieve same result?
postgres allows boolean values in order by clause, here generalised 'x last':
order (my_column = 'x') the expression evaluates boolean, resulting values sort way:
false (0) true (1) null since deal non-null values, that's need. here one-liner:
... order (zone = 'future'), zone, status; related:
Comments
Post a Comment