Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 具有多列的Order By子句的CASE语句_Oracle - Fatal编程技术网

Oracle 具有多列的Order By子句的CASE语句

Oracle 具有多列的Order By子句的CASE语句,oracle,Oracle,我希望ORDERBY语句根据条件更改列排序顺序 例如,如果有A、B、C、D列,并且条件值作为参数输入(pType) 如果pType=1,则排序为A、B、C、D 如果pType=2,则排序为B、C、D、A 如果pType=3 C、A、B、D 当语句按如下方式应用和使用时,按情况排序。 发生了一个错误 Order by Case when pType = 1 then A, B, C, D when pType = 2 then B, C, D, A when pType = 3 then

我希望ORDERBY语句根据条件更改列排序顺序

例如,如果有A、B、C、D列,并且条件值作为参数输入(pType)

  • 如果pType=1,则排序为A、B、C、D

  • 如果pType=2,则排序为B、C、D、A

  • 如果pType=3 C、A、B、D

当语句按如下方式应用和使用时,按情况排序。 发生了一个错误

Order by
Case
when pType = 1 then A, B, C, D
when pType = 2 then B, C, D, A
when pType = 3 then C, A, B, D
我应该如何使用它

谢谢你的帮助。

看看这是否有效

with t1_ as
(
  select 1 as ptype, 10 as a, 5  as b, 20 as c from dual union all
  select 1 as ptype, 6  as a, 10 as b, 21 as c from dual union all
  
  select 2 as ptype, 20 as a, 2 as b, 16 as c from dual union all
  select 2 as ptype, 100 as a, 1 as b, 90 as c from dual
)

select 
* 
from t1_
order by
case when ptype = 1 then a end asc,
case when ptype = 1 then b end asc,
case when ptype = 1 then c end asc,

case when ptype = 2 then b end asc,
case when ptype = 2 then c end asc,
case when ptype = 2 then a end asc
FIDLE url供参考

您可以使用以下功能:

ORDER BY DECODE(pType, 1, A, 2, B, 3, C),
         DECODE(pType, 1, B, 2, C, 3, A),
         DECODE(pType, 1, C, 2, D, 3, B),
         DECODE(pType, 1, D, 2, A, 3, D)