PostgreSQL - 条件排序

PostgreSQL - conditional ordering

我有以下 table:

key | date         | flag
--------------------------
1    now()           true
2    now() - 1 hour  true
3    now() + 1 hour  true
4    now()           false
5    now() - 1 hour  false
6    now() + 1 hour  false

我想要以下排序:

下面的查询是否正确?

(
    select *
    from test
    where flag = false
    order by date asc
)
union all
(
    select *
    from test
    where flag = true
    order by date desc
)

有更好的方法吗? union all 是否会保持行排序,因此只是连接两个内部查询的输出?

我不知道如何根据条件重复 order by 中的列。

更新

Fiddle 可以在这里找到:http://rextester.com/FFOSS79584

可以使用 CASE 执行条件订单,如下所示:

select *
    from test
order by 
    flag
  , case when flag then date end desc
  , case when not flag then date end asc