Informix子​​查询 - 如何正确执行它们

时间:2013-12-09 17:35:23

标签: sql informix

我正在研究Informix查询,1)给我一个呼叫中心代理列表,2)给我他们最近的状态。我的查询几乎可以完成我需要的所有内容;

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

然而,这将给我一个“eventtype”的完整记录(eventtype显示代理在给定时间所处的状态)。我试图只拉动最近的状态。所以,我认为我需要对这个主查询做一个子查询,说明“如果在这个子查询中找到主查询的输出,那就保持它”。子查询看起来像这样;

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

这个子查询本身几乎是我需要的一切,但它没有eventtype。这个查询将如何运作?

这是我通常编写子查询但不能在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

1 个答案:

答案 0 :(得分:1)

尝试使用该子查询进行内部联接,并在agentid上执行agentstatedetail并找出最大值的日期:

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

我对你想在最后看到的列感到有点困惑。如果不是这样,则必须在内部和外部查询中更改它们。