表格:
NUMBER DETAIL_ID,
Date schdeuledDate;
Date completedDate;
NUMBER recordId;
我正在尝试实现这一点,检索记录的最近完成日期;如果为null,则检索最近的计划日期;如果检索到多个记录,则使用更高的recordId。
SELECT DETAIL_ID,completedDate,schdeuledDate,recordId FROM table
ORDER BY CASE completedDate WHEN NULL THEN schdeuledDate ELSE completedDate END,DETAIL_ID,recordId DESC;
但这是我从上面的查询中得到的
Detail_ID compdate schdate recordId
318 08-AUG-16 15-AUG-16 22342
318 06-JUN-18 22-AUG-18 26219
318 22-AUG-17 08-AUG-17 26218
418 null 08-AUG-17 26450
418 22-AUG-17 22-AUG-18 26879
418 22-AUG-17 22-AUG-18 26779
预期:
318 06-JUN-18 22-AUG-18 26219
418 22-AUG-17 22-AUG-18 26879
答案 0 :(得分:1)
我认为您想要row_number()
:
select t.*
from (select t.*,
row_number() over (partition by detail_id
order by coalesce(schdeuledDate, completedDate) desc, recordId desc
) as seqnum
from t
) t
where seqnum = 1;