我需要弄清楚同一行上两个时间结果列之间的区别?

时间:2013-10-08 10:36:21

标签: mysql

我使用的查询:

 SELECT 
   (select axpuser.loginid from axpuser where axpuser.pkey = USERID) as agent,
   (select breakcode.NAME from breakcode where breakcode.pkey = statecode) as breakcode,
   PKEY AS StartKey,
   TIMEOFSTATECHANGE AS StartTime, 
   (SELECT TIMEOFSTATECHANGE FROM agentstatusinformation WHERE PKEY > StartKey AND
   AGENTSTATE = 1  LIMIT 1 ) AS EndTime
FROM agentstatusinformation
WHERE DATEOFSTATECHANGE = '2013-10-7' AND AGENTSTATE = 3 AND STATECODE <> 0
ORDER BY TIMEOFSTATECHANGE ASC;

示例结果:

 agent      breakcode       Startkey     StartTime   Endtime
 David      Programming     73945        09:31:58   10:06:31
 David      Comfort         73950        10:19:57   10:20:58
 John       Comfort         73960        10:58:11   11:02:12
 Stan       Comfort         73998        12:02:17   12:02:18

我遇到的问题是计算EndTime和Start Time之间的时差。 PKEY 从我的表中是唯一递增的ID,但不是并发的,换句话说startPKey可能是73945,代理的下一个密钥可能是74001,我想不到任何联接使用。

下面的表格示例:

 INSTIME                 PKEY USERID AGENTCALLINFOID    AGENTSTATE DATE TIME STATECODE
 2013-10-07 10:19:57    73950   17          0               3   2013-10-07 10:19:57 1   
 2013-10-07 10:20:42    73951   11          24364           4   2013-10-07 10:20:42 0   
 2013-10-07 10:20:58    73952   11          0               1   2013-10-07 10:20:58 8   
 2013-10-07 10:27:45    73953   17          0               1   2013-10-07 10:27:45 0   
 2013-10-07 10:32:59    73954   11          24365           2   2013-10-07 10:32:59 0   

0 个答案:

没有答案