Question

I'm working on pulling agent states from a call center system and would like to know if there's a way, in Informix, to pull only the most recent entry of a value from a table.

The table I'm working on registers the changing state for agents, which I would like to present on a web page. However, it registers every single change. This is the 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 'teamnamehere %') 
group by a.eventdatetime
    ,b.resourcename
    ,b.extension
    ,a.eventtype 
order by eventdatetime desc

The query works fine, but I am only interested in resourcename's most current entry, ignoring all the older ones. Is this possible with Informix?

Was it helpful?

Solution

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.

OTHER TIPS

If your query runs slowly then use explain to see the reason and also analyse the index usage. it might be the:

date(eventdatetime) = date(current)

condition. if you have a nice index on eventdatetime (which is datetime year to second?) then perhaps use it this way:

eventdatetime>='yyyy-mm-dd 00:00:00' and eventdatetime<='yyyy-mm-dd 23:59:59'

where yyyy-mm-dd = today

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top