I want to order a Postgres query depending on conditions in the table
id sensor_id alarmTime clearedTime
415068 3734 2016-04-20 12:25:32 2016-04-27 21:05:44
415070 3734 2016-04-21 12:25:32 null
415071 3734 2016-03-22 12:25:32 2016-04-29 21:15:44
415072 3734 2016-04-23 12:25:32 null
415073 3734 2016-04-20 12:25:32 2016-04-22 22:05:45
415074 3734 2016-02-26 12:25:32 2016-04-27 23:15:24
From the above table I want to initially order by nulls first and alarm time with the remaining rows ordered by cleared time only. to get some thing like this
id sensor_id alarmTime clearedTime
415070 3734 2016-04-21 12:25:32 null
415072 3734 2016-04-23 12:25:32 null
415068 3734 2016-04-20 12:25:32 2016-04-27 21:05:44
415073 3734 2016-04-20 12:25:32 2016-04-22 22:05:45
415071 3734 2016-03-22 12:25:32 2016-04-29 21:15:44
415074 3734 2016-02-26 12:25:32 2016-04-27 23:15:24
I have tried something like this
select * from table
ORDER BY
case alarm.clearedTime is NOT NULL
when TRUE THEN alarmTime
else clearedTime
end DESC NULLS FIRST;
415072shoul be first than415070