my table looks like this:
eventname varchar(255)
open int unixtimestamp
close int unixtimestamp
i like to sort the table by soonest, which means if its a coming event (open and close < now()) PostgreSQL should order the table by open, but if its a current or past event (close > now()) it should order the table by close
i tried to archive this with following order by clause:
ORDER BY case when close>1423053440 and open>1423053440 then 'open desc' else 'close desc' END
which basically don't work, because the planer don't support asc/desc decoration inside a order by case
any hints highly appreciated
best regards
andreas
Use multiple keys:
ORDER BY (case when close > 1423053440 and open > 1423053440 then 1 else 2 end),
(case when close > 1423053440 and open > 1423053440 then open end) desc,
close asc;
The first expression puts the opens first. The second orders the open
s descending. The third orders the rest using close
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.