答案 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>