Of course. You just need a sub-query to identify the most recent record for each agent. Something like (untested):
select a.eventdatetime
,b.resourcename
,b.extension
,a.eventtype
from agentstatedetail as a
,resource as b
,team as c
,(SELECT agentid, MAX(eventdatetime) AS lastevent
FROM agentstatedetail
WHERE DATE(eventdatetime) = TODAY
GROUP BY agentid) AS d
where (a.agentid = b.resourceid)
and (b.assignedteamid = 10)
and (c.teamname like 'teamnamehere %')
and (d.agentid = a.agentid and a.eventdatetime = d.lastevent)
group by a.eventdatetime
,b.resourcename
,b.extension
,a.eventtype
order by eventdatetime desc
You may need to look at indexing agentstatedetail
to get maximum efficiency.
EDIT
Per your comment about avoiding the nested query and handling the skipping of agentid values already seen, that's a fairly trivial client-side solution. I don't know exactly how you're handling this on the PHP side, but you'd basically want to do something like this:
$data = $db->query("select a.eventdatetime, b.resourcename, b.extension, a.eventtype
from agentstatedetail as a, resource as b, team as c
where date(eventdatetime) = date(current)
and (a.agentid = b.resourceid) and (b.assignedteamid = 10)
and (c.teamname like 'ITS Help Desk %')
group by a.eventdatetime, b.resourcename,
b.extension, a.eventtype
order by eventdatetime desc");
$agent = Array();
foreach($data as $row){
if(!$agent[$row['RESOURCENAME']]++) {
echo
"<TR bgcolor='#D0D0D0'><TD class='body'>" . $row['RESOURCENAME'] .
"<TD class='body'>" . $row['EVENTTYPE'] .
"</TD>";
}
}
The associative array $agent
tracks how many records have been seen for a particular agent. When that's empty, it's the first occurrence. The exact non-zero number is not really useful, we just use a post-increment for efficiency, rather than setting $agent[$row['RESOURCENAME']]
explicitly in the loop.