2013-09-25 57 views
1

從下面的查詢中,我想查找包含每個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; 

回答

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;