简体   繁体   English

PostgreSQL订单的打开和关闭日期

[英]postgresql order by open and closing date

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 我想按最快的顺序对表进行排序,这意味着它的即将发生的事件(打开和关闭<now())PostgreSQL应该按打开的顺序对表进行排序,但是如果它是当前或过去的事件(关闭> now()),则应该通过关闭命令表

i tried to archive this with following order by clause: 我尝试使用以下order by子句将其归档:

   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 基本上是行不通的,因为刨床不支持按个案排序的asc / desc装饰

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. 第二个命令open的降序。 The third orders the rest using close . 第三个命令使用close排序其余的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM