如何在Postgres中为联合查询设置自定义排序顺序

时间:2015-06-24 10:01:35

标签: postgresql sql-order-by union

使用这样的查询(为简洁起见而简化):

SELECT 'East' AS name, *
FROM events 
WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' 

UNION

SELECT 'West' AS name, *
FROM events 
WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00'

UNION

SELECT 'Both' AS name, *
FROM events 
WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00'

我想自定义结果行的顺序。类似的东西:

ORDER BY name='East', name='West', name='Both'

或者

ORDER BY 
    CASE
        WHEN name='East' THEN 1 
        WHEN name='West' THEN 2
        WHEN name='Both' THEN 3
        ELSE 4
    END;

然而,Postgres抱怨说:

ERROR:  invalid UNION/INTERSECT/EXCEPT ORDER BY clause
DETAIL:  Only result column names can be used, not expressions or functions.
HINT:  Add the expression/function to every SELECT, or move the UNION into a FROM clause.

我有其他选择吗?

3 个答案:

答案 0 :(得分:22)

将其包装在派生表中(这是“ HINT:......或将UNION移动到FROM子句中”建议)

select *
from (
  ... your union goes here ... 
) t
order by
    CASE
        WHEN name='East' THEN 1 
        WHEN name='West' THEN 2
        WHEN name='Both' THEN 3
        ELSE 4
    END;

答案 1 :(得分:12)

我添加了一个显示所需排序的额外列,然后使用ORDER BY中的序号列位置,例如

SELECT 1, 'East' AS name, *
...
UNION ALL
SELECT 2, 'West' AS name, *
...
ORDER BY 1

请注意,您可能还需要UNION ALL,因为您添加的列确保联合中的每个集合都必须是不同的。

答案 2 :(得分:0)

通过添加额外的列来进行排序,但是它使UNION子句完全像UNION ALL一样工作(它不会从结果中消除重复的行)。