Question

I'm working on an Informix query that wiil 1) Give me a list of call center agents and 2) give me their most recent status. I have a query that does almost everything I need;

select b.resourcename, b.extension, a.eventtype
from agentstatedetail as a, resource as b, team as c
where date (eventdatetime) = TODAY
  and (a.agentid = b.resourceid)
  and (b.assignedteamid = c.teamid)
  and (c.teamname like 'Team Name %')
group by b.resourcename, b.extension, a.eventtype
order by resourcename asc

However, this will give me a complete record of "eventtype" (eventtype shows the states an agent was in at a given time). I'm trying to pull only the most recent state. So, I believe I need to do a subquery to this main query stating something like, "If the main query's output is found in this subquery, then keep it". The subquery looks like this;

select a.agentid, b.resourcename, c.teamname, max(a.eventdatetime)
from agentstatedetail as a, resource as b, team as c
where date (eventdatetime) = TODAY
  and a.agentid = b.resourceid
  and b.assignedteamid = c.teamid
  and c.teamname like 'Team Name %'
group by a.agentid, b.resourcename, c.teamname

This subquery, by itself, is almost everything I need, but it doesn't have the eventtype. How would this query work?

This is the format that I typically write subqueries in but can't make work in Informix;

select a.agentid, b.resourcename, c.teamname, max(a.eventdatetime) as eventdatetime, a.eventtype from agentstatedetail as a, resource as b, team as c where date(eventdatetime) = TODAY and a.agentid=b.resourceid and b.assignedteamid=c.teamid and c.teamname like 'ITS %' and where a.eventdatetime in (select a.agentid, b.resourcename, c.teamname, max(a.eventdatetime) from agentstatedetail as a, resource as b, team as c where date(eventdatetime) = TODAY and a.agentid=b.resourceid and b.assignedteamid=c.teamid and c.teamname like 'Team Name %' group by a.agentid, b.resourcename, c.teamname) group by a.agentid, b.resourcename, c.teamname, a.eventtype order by max(a.eventdatetime) desc
Was it helpful?

Solution

Try doing an inner join with that subquery and agentstatedetail on agentid and the date that you found out that was the max:

select m.agentid, m.resourcename, m.teamname, a2.eventtype
from (
  select a.agentid,b.resourcename,c.teamname,max(a.eventdatetime) as maxtdate
  from agentstatedetail as a
  inner join resource as b on a.agentid = b.resourceid
  inner join team as c on b.assignedteamid = c.teamid
  where date (eventdatetime) = TODAY
    and c.teamname like 'Team Name %'
  group by a.agentid, b.resourcename, c.teamname
  ) m
inner join agentstatedetail a2 
    on a2.agentid = m.agentid and a2.eventdatetime = m.maxtdate

I'm a little confused on the columns that you want to see at the end. If these are not it, you will have to change them in the inner and outer queries.

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