postgresql - PostgreSQL 条件多重排序 by with asc/desc
问题描述
我想有条件地按多列排序,但不仅是所有列的单一排序类型(如此处所示)。
我想完成这样的事情(无效的sql,因为我不知道该怎么做,只是想传达这个想法):
-- assume this is wrapped by a function with a `_sort` string argument
select * from t_test
order by
case when _sort='sort_type_1' then (
col1 desc, col2 asc, col3 desc, col4 asc)
case when _sort='sort_type_2' then (
col1 asc, col2 asc, col3 desc, col4 asc)
case when _sort='sort_type_3' then (
col1 desc, col2 desc, col3 asc, col4 asc)
else (
col4 desc, col3 asc)
end;
请注意 , , , 的顺序col1
很col2
重要col3
,col4
所以我不能做这样的事情:
select * from t_test
order by
col2, col4
case when _sort='sort_type_1' then (col1, col3) end desc;
这是否可以通过类似的方法实现,或者是否需要完全不同的方法?
解决方案
推荐阅读
- azure - Azure AD SAML 2.0 令牌未加密
- reactjs - 触发 drop 事件时设置状态不起作用
- spring - 使用 Spring Boot 实现 JsonApi
- azure - Azure 网络接口可以归类为计算资源还是仅仅是网络资源?
- typescript - 如何在 typeorm 中创建关系的正确方法是什么?
- controller - 帐户的标准控制器空结果
- java - 无法验证 Maven 以从 ADO 管道访问 Azure 工件
- java - 从Java中的小数点前后提取数字值
- php - Laravel 完整日历
- sql - 使用另一个表中的信息更新 SQL Server 中的表