看来我走到了尽头。我从Informix数据库(cisco呼叫中心数据)中提取数据,并尝试编写一个查询,显示每个代理状态的持续时间。表格中有4列(agentstatedetail)我来自:agentid,eventdatetime,eventtype和reasoncode。查询的目标是获取每个事件类型的持续时间。查询运行正常,但有一个主要缺陷。当代理状态(对于相同的agentid)同时发生时(下一个eventtype的eventdatetime相同),我的查询返回重复项,因为在我的子查询中我有eventdatetime> asd1.eventdatetime。基本上我需要创建第二个eventdatetime列并将每行向上移动1,其中agentid = agentid。我尝试使用MIN函数但是如上所述,这个逻辑是有缺陷的,因为有些事件同时发生。我在下面粘贴了我的代码。我已经尝试创建一个索引表,因为我无法为我写这个不是一个选项的数据库。 另一种说法是,无论agentid = agentid在哪里,我怎么说下一行?任何建议都会非常有用。谢谢!
PhotoLinks: https://goo.gl/photos/sqM8GYdDZLrD3jAAA:QueryOutcome https://goo.gl/photos/5E68jvwadjA5y5zZA:通缉结果 - 即21个原因代码=午餐
表架构: (agentID:状态已更改的代理的标识符,int,NOT NULL,主键)
(eventDateTime:代理状态更改的日期和时间.datetime year to fraction(3),NOT NULL,Primary Key)
(eventType:触发代理状态更改的事件:smallint,NOT NULL,主键)
(reasonCode:如果未配置原因代码,则为空,smallint,NOT NULL,主键)
select res.resourcename,
DATE(asd1.eventdatetime) as Date,
asd1.eventdatetime as starttime,
asd2.eventdatetime as endtime,
((asd2.eventdatetime-asd1.eventdatetime)::interval second(9) to second::char(10)::int) as duration,
asd1.reasoncode as reasoncodenum
from agentstatedetail asd1
join agentstatedetail asd2
on asd1.agentid=asd2.agentid
and asd2.eventdatetime = (select min(eventdatetime)
from agentstatedetail
where agentid = asd1.agentid
and eventdatetime>asd1.eventdatetime)
left join resource res on asd1.agentid=res.resourceid
where asd1.agentid=asd2.agentid
答案 0 :(得分:0)
所以给定的架构是:
create table agentstatedetail
(
agentid integer,
eventdatetime datetime year to fraction(3),
eventtype smallint,
reasoncode smallint,
primary key (agentid,eventdatetime,eventtype,reasoncode)
);
您只有下一个 eventtype :
1 'Logged In'
2 'Not Ready'
3 'Ready'
4 'Reserved'
5 'Talking'
6 'Work'
7 'Logged Out'
你想要报告 eventtype 之间的转换,即使权利是0秒。
问题是当你有0秒时,你会得到重复。
这些规则是否正确:
如果所有这些规则都正确,则下面的测试用例应该有效:
CREATE TABLE agentstatedetail (
agentid INT,
eventtype INT,
eventdatetime DATETIME YEAR TO FRACTION
);
INSERT INTO agentstatedetail VALUES (1, 1, CURRENT - 1 UNITS HOUR);
INSERT INTO agentstatedetail VALUES (1, 2, CURRENT);
INSERT INTO agentstatedetail VALUES (1, 3, CURRENT);
INSERT INTO agentstatedetail VALUES (1, 4, CURRENT + 1 UNITS HOUR);
SELECT
t1.agentid AS agent_id,
t1.eventtype AS src_event_type,
t2.eventtype AS dst_event_type,
t1.eventdatetime AS start_time,
t2.eventdatetime AS stop_time,
(t2.eventdatetime - t1.eventdatetime ) AS durantion
FROM
agentstatedetail t1 JOIN agentstatedetail t2
ON t1.agentid = t2.agentid
AND t2.eventdatetime = (
SELECT MIN(eventdatetime )
FROM agentstatedetail
WHERE agentid = t1.agentid
AND eventdatetime >= t1.eventdatetime
AND NOT (
(t1.eventtype = eventtype )
OR (t1.eventtype = 1 AND eventtype = 3)
OR (t1.eventtype = 3 AND eventtype = 2)
OR (t1.eventtype = 2 AND eventtype = 4)
)
)
WHERE
NOT (
(t1.eventtype = t2.eventtype )
OR (t1.eventtype = 1 AND t2.eventtype = 3)
OR (t1.eventtype = 3 AND t2.eventtype = 2)
OR (t1.eventtype = 2 AND t2.eventtype = 4)
);