具有最大和空值的案例陈述

时间:2018-08-13 11:52:33

标签: plsql plsqldeveloper

我有一张桌子:

enter image description here

我需要选择拖曳c_id,其中

  • 日期值是null和
  • 日期列中的
  • 最大日期。

DATE列中可以有两个以上的日期值。

将选择c_id 3 4

我如何在单个case语句中写这个?

1 个答案:

答案 0 :(得分:0)

分析可能会有所帮助;以DATE列降序排列值,并附加NULLS FIRST子句。例如:

SQL> with test (a_id, c_id, c_date) as
  2    (select 1, 2, date '2017-05-20' from dual union all
  3     select 1, 3, date '2017-07-17' from dual union all
  4     select 1, 4, null              from dual
  5    )
  6  select a_id, c_id, c_date
  7  from (select a_id, c_id, c_date,
  8          row_number () over (partition by a_id
  9                              order by c_date desc nulls first) rn
 10        from test
 11       )
 12  where rn <= 2;

      A_ID       C_ID C_DATE
---------- ---------- ----------
         1          4
         1          3 2017-07-17

SQL>