具有最大值和空值的案例陈述
Case statement with max and null value
我有一个 table:
我需要 select 拖车 c_id
- 日期值为空且
- 日期列中的最大日期。
DATE
列中可以有超过 2 个日期值。
输出将是 select c_id
3 & 4.
如何在单个 case 语句中写这个?
分析可能会有所帮助;按 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>
我有一个 table:
我需要 select 拖车 c_id
- 日期值为空且
- 日期列中的最大日期。
DATE
列中可以有超过 2 个日期值。
输出将是 select c_id
3 & 4.
如何在单个 case 语句中写这个?
分析可能会有所帮助;按 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>