select o.AgentId ,o.Date,o.profit from agentprofit as o join (select a.AgentId,MAX(Date) as max_Date from agentprofit as a GROUP BY a.AgentId) as t on o.AgentId=t.AgentId and o.Date=t.max_Date GROUP BY o.AgentId,o.profit