排序< = 37天加上其他要求?

时间:2015-02-28 23:22:28

标签: sql oracle sorting grouping

我有一个请求按以下方式对表进行排序,首先查看1,然后是2,然后是3:

  1. sample_date< =从当前日期起37天(最近的日期为先)
  2. 填充任何文本field_x(空白字段应位于底部)
  3. number_field中的数字从最大到最小(递减)排序
  4. 这可能吗? #1是问题,但我也遇到了问题#2,当排序desc时,空白排在第一位。

    这是oracle db中的sql,将显示在power builder应用程序中。谢谢!

1 个答案:

答案 0 :(得分:1)

只需在order by中使用多个键即可。你的规则是这样的:

order by (case when sample_date <= 37 then 1
               else 2 end),
         (case when field_x is not null then 1 else 2 end),
         (case when sample_date <= 37 then sample_date end) desc,
         number desc

如问题所述,规则含糊不清。例如,field_x中具有最近日期和空值的行在哪里?但是,case语句和多个键的使用应该指向正确的方向。