尝试:
select mail_id, archived_by, archived_date, cnt
from (
select mail_id, archived_by, archived_date, count(*) cnt
from Mail_Archives
group by mail_id, archived_by, archived_date
order by archived_date desc)
where rownum = 1;
或者(如果你喜欢窗口函数)
select mail_id, archived_by, archived_date, cnt from (
select mail_id,
archived_by,
archived_date,
row_number() over (order by archived_date desc) rn,
count(*) over (partition by mail_id, archived_by, archived_date) cnt
from Mail_Archives)
where rn = 1;
这是一个 sqlfiddle 演示
更新假设您只需要 mail_id 的最大日期和计数
select mail_id, archived_by, archived_date, cnt from (
select mail_id,
archived_by,
archived_date,
row_number() over (order by archived_date desc) rn,
count(*) over (partition by mail_id) cnt
from Mail_Archives)
where rn = 1;
这是一个 sqlfiddle 演示