G
G
GDim2016-04-03 12:47:32
PostgreSQL
GDim, 2016-04-03 12:47:32

How to ignore negative values ​​when sorting PostgreSQL?

When sorting values, you must ignore the values ​​\u003d -1 by type NULLS LAST.
Something like:
SELECT * FROM table ORDER BY column -1 LAST

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Shelemetiev, 2016-04-03
@GDim

order by case when column = -1 then null else column end nulls last
or even simpler:
order by nullif(column,-1) nulls last

R
res2001, 2016-04-03
@res2001

Add an extra field that calculates like this:
case column
when -1 then max(column)
else column
end
and sort by that field, not by column.
maybe instead of max(column) you need to put min(column) or just take a constant value (maximum or minimum).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question