I have two tables..
1-Identity(id, ref1,ref2, address)
2-details(ref1,ref2,amount,u_no,u_date)
I want to extract the each id with sum of amount having highest u_date and highest u_no
i tried below--
Select I.id, d.amount
From identity I Inner Join
(select ref1,ref2,sum(amount) as amount
From details d
where (ref1,ref2,u_no,u_date) In (select ref1, ref2, max(u_no) as u_no, max(u_date) as u_date from details group By ref1,ref2)
Group By ref1,ref2)
) d
On I.ref1 = d.ref1 And I.ref2 = d.ref2;
But I am getting same id with multiple amount. Table details and expected output
Can someone plz help me with this.thanks