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

SQL - Find time duration duration with only eventstarttime (query)

我想知道如何查询事件之间的持续时间。我正在查看的 table 名为 agentstatedetail。为了简单起见,在 table 中有 2 列,agentideventdatetime。 table 列出了 agentid 和事件开始日期和时间。在每个事件之后,都会记录一个新事件以及新的事件日期和时间。我正在尝试从与同一代理匹配的先前 eventdatetime 中减去最新的 eventdatetime。我无法粘贴 table 的图片,因为我是新手,但我已经粘贴了我使用过的代码。谢谢,希望有人能帮忙!!!

我尝试了 2 个查询示例,但它们都不起作用。注意:这是在 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
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)

select asd1.agentid, asd1.eventtype, asd2.eventdatetime, asd1.eventdatetime, ((asd2.eventdatetime-asd1.eventdatetime::interval minute(9) 到 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 = (select min(eventdatetime) 来自 agentstatedetail 其中 agentid = asd1.agentid 和事件日期时间 > asd1.eventdatetime) 其中 asd1.agentid=1953 和 asd1.eventdatetime 在“2015-5-1 13:00:00”和“2015-5-2 13:00:00”之间