从Union All获取最大日期行

时间:2013-09-25 18:35:26

标签: oracle subquery max union

从下面的查询中,我想找到每个request_id的最后一个activity_dt的事件。我已经工作了几个小时,但无济于事。有人可以帮忙吗?

select a.event as event, a.activity_dt as activity_dt, b.request_id as request_id
from gs_pr_obl_event a, gs_pr_obl b
where a.obligation_id = b.obligation_id
union all
select event as event, activity_dt as activity_dt, request_id as request_id
from gs_pr_req_event;

1 个答案:

答案 0 :(得分:3)

试试这个,

SELECT event, MAX(activity_dt), request_id
FROM(
     SELECT A.event AS event, A.activity_dt AS activity_dt, b.request_id AS request_id
     FROM gs_pr_obl_event A, gs_pr_obl b
     WHERE A.obligation_id = b.obligation_id
     UNION ALL
     SELECT event AS event, activity_dt AS activity_dt, request_id AS request_id
     FROM gs_pr_req_event
     )
GROUP BY event, request_id;