SQL - 仅使用eventstarttime查询持续时间(查询)

时间:2015-05-21 17:33:39

标签: sql informix duration

我试图弄清楚如何查询事件之间的持续时间。我正在查看的表格称为 agentstatedetail 。为简单起见,在表格中有2列, agentid eventdatetime 。该表列出了agentid和事件开始日期和时间。在每个事件之后,将记录一个新事件,其中包含新的事件日期和时间。我试图从先前与同一代理匹配的 eventdatetime 中减去最新的 eventdatetime 。我无法粘贴表格的图片,因为我是新的,但我已经粘贴了我已经处理过的代码。谢谢,我希望有人能帮忙!!!

我尝试了两个查询示例,但它们不起作用。注意:这是在SQL Informix中,但我不在乎是否有人可以解决一些问题,即使它是在MySQL中。

select  asd1.*, asd2.* from agentstatedetail asd1
 join agentstatedetail asd2 
on asd1.agentid=asd2.agentid
 and asd1.eventdatetime < asd2.eventdatetime
and 0=(select count(*) from agentstatedetail asd3
    where asd3.agentid =asd1.agentid
        and asd3.eventdatetime between asd1.eventdatetime and asd2.eventdatetime
)
where asd1.eventdatetime BETWEEN '2015-5-1 13:00:00' AND '2015-5-2    13:00:00'
and asd2.eventdatetime BETWEEN '2015-5-1 13:00:00' AND '2015-5-2 13:00:00'
and asd1.agentid=1953 


select  asd1.*, asd2.* 
from agentstatedetail asd1
join agentstatedetail asd2 
on asd1.agentid=asd2.agentid
and asd1.eventdatetime < asd2.eventdatetime

left join agentstatedetail asd3
on asd3.agentid =asd1.agentid
    and asd3.eventdatetime between asd1.eventdatetime and       asd2.eventdatetime
where asd1.eventdatetime BETWEEN '2015-5-1 13:00:00' AND '2015-5-2 13:00:00'

 and asd1.agentid=1953 
and asd3.agentid is null

2 个答案:

答案 0 :(得分:0)

Select b.agentId, 
   b.eventdatetime start,
   e.eventdatetime end,
   e.eventdatetime - b.eventdatetime ElapsedTime
From agentstatedetail b
    left join agentstatedetail e
         on e.agentid = b.agentid
            and e.eventdatetime =
                (Select Min(eventdatetime)
                 from agentstatedetail 
                 where agented = b.agentid
                     and eventdatetime > b.eventdatetime)

答案 1 :(得分:0)

选择asd1.agentid,asd1.eventtype,asd2.eventdatetime,asd1.eventdatetime, ((asd2.eventdatetime-asd1.eventdatetime):: interval minute(9)to minute :: char(10):: int)作为IntervalMinutes, ((asd2.eventdatetime-asd1.eventdatetime):: interval second(9)to second :: char(10):: int)As Interval

来自agentstatedetail asd1    加入agentstatedetail asd2    在asd1.agentid = asd2.agentid      和asd2.eventdatetime =(选择min(eventdatetime)                                来自agentstatedetail                                其中agentid = asd1.agentid                                和eventdatetime&gt; asd1.eventdatetime) 其中asd1.agentid = 1953年 asd1.eventdatetime BETWEEN&#39; 2015-5-1 13:00:00&#39; AND&#39; 2015-5-2 13:00:00&#39;