我需要进行自定义 Oracle 排序

I need to do a Custom Oracle Sort

如果这是一个简单的问题,我很抱歉。但是,我在大约一个小时的时间里到处寻找答案。

我有一个 db_table,我需要对它进行排序以便在网页中输出到 table。数据以下列方式存储在 db_table 中:

   date          Area         Value
   ------        ------      -------
 11-mar-18        middle        10
 11-mar-18        bottom         5
 11-mar-18        top           12

 12-mar-18        top           14
 12-mar-18        bottom         4
 12-mar-18        middle        17

问题是:如何对这些进行排序以产生以下结果:

    date          Area         Value
   ------        ------      -------
 11-mar-18        top           12 
 11-mar-18        middle        10
 11-mar-18        bottom         5 

 12-mar-18        top           14
 12-mar-18        middle        17
 12-mar-18        bottom         4

非常感谢任何帮助。

按字母顺序降序排列区域作为 的第二个组成部分 排序:

select *
  from db_table
 order by "date", area desc;

正如您提到的,如果区域具有值 A、B、C、D,并且他们希望按 C、B、D、A 的顺序排序,则使用:

select *
  from db_table
 order by "date", decode(area,'C',1,'B',2,'D',3,'A',4);

P.S。特别是,我将日期列放在引号内,因为已经用 "date" 列创建了 table,而不是 date,这作为关键字是不可能的。

select *
  from db_table
 order by "date", 
    case 
       when area = 'top' then 1 
       when area = 'middle' then 2
       when area = 'bottom' then 3
       else 4
    end;

参考Custom Sort Order

在大多数情况下,

instr() 提供比 case 更短的解决方案:

select t.*
from t
order by t.date, instr('top,middle,bottom', area)