Angelo M
2012-07-23 16:27:03 UTC
C_ID | REQ_ID | EXP_DATE
24 211 Jun-18-12
39 211 Jun-18-12
26 211 April-7-12
So what I need is to choose the req_id with the latest date. But in the case of a tie, the tie is broken by choosing the record with the highest c_id. So in the example above, the result would be:
C_ID | REQ_ID | EXP_DATE
39 211 Jun-18-12
So far I'm doing something like this: (pseudo-code)
select max(c_id), req_id, exp_date
from (select req_id, max(exp_date)
from table
group by req_id) t1
join
(select c_id, req_id, exp_date
from table) t2 on (t1.exp_date = t2.exp_date)
group by req_id, exp_date
I'm using PL/SQL. It works, but its not very efficient. I'm wondering if anyone can help me make this better. Thank you very much.