我正在努力从呼叫中心系统中提取代理状态,并想知道在Informix中是否有一种方法只从表中提取值的最新条目。
我正在处理的表记录了代理的更改状态,我想在网页上显示。但是,它会记录每一个变化。这是查询;
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
查询工作正常,但我只对resourcename的最新条目感兴趣,忽略所有旧条目。 Informix可以实现吗?
答案 0 :(得分:1)
当然。您只需要一个子查询来标识每个代理的最新记录。像(未经测试)的东西:
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
您可能需要查看索引agentstatedetail
以获得最高效率。
修改强>
根据你关于避免嵌套查询和处理跳过已经看到的agentid值的评论,这是一个相当简单的客户端解决方案。我不确切知道你是如何在PHP方面处理这个问题的,但你基本上想做这样的事情:
$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>";
}
}
关联数组$agent
跟踪特定代理程序的记录数。当它为空时,它是第一次出现。确切的非零数字并不真正有用,我们只是使用后增量来提高效率,而不是在循环中显式设置$agent[$row['RESOURCENAME']]
。
答案 1 :(得分:0)
如果您的查询运行缓慢,请使用explain查看原因并分析索引使用情况。它可能是:
日期(eventdatetime)=日期(当前)
条件。如果你在eventdatetime上有一个很好的索引( datetime year to second ?),那么也许这样使用它:
eventdatetime&gt; ='yyyy-mm-dd 00:00:00'和eventdatetime&lt; ='yyyy-mm-dd 23:59:59'
其中yyyy-mm-dd =今天