我的数据如下:
TicketID Status Datetime
25 Open 2016-07-21 11:46:40
25 Open 2016-07-22 09:42:40
25 Closed 2016-07-22 09:46:40
25 Open 2016-07-23 08:42:40
25 Open 2016-07-24 08:46:40
25 Open 2016-07-24 09:26:40
25 Closed 2016-07-25 11:46:40
我想计算以下内容:
<(>(2016-07-22 09:46:40 - 2016-07-21 11:46:40)+(2016-07-25 11:46:40 - 2016-07-23 08:42:40 ))/ 2。答案 0 :(得分:1)
这是一个想法:对于每个“开放”获得下一个“关闭”。然后只选择“关闭”值并采用最小值打开以确定时间长度。因此,以下内容可以获得您想要的一对次数:
select close_datetime, min(datetime) as open_datetime
from (select t.*,
(select t2.DateTime
from t t2
where t2.status = 'Closed' and t2.DateTime > t.DateTime
) as close_datetime
from t
where status = 'Open'
) t
group by close_datetime;
然后您可以获得差异的总和。这将以秒为单位获取值:
select sum(timestampdiff(second, clsoe_datetime, open_datetime) as num_seconds
from (select close_datetime, min(datetime) as open_datetime
from (select t.*,
(select t2.DateTime
from t t2
where t2.status = 'Closed' and t2.DateTime > t.DateTime
) as close_datetime
from t
where status = 'Open'
) t
group by close_datetime
) t
答案 1 :(得分:0)
这对我来说似乎没问题。
set @x=now(), @y=-1;
select
avg(timestampdiff(second,prev_stat_dt,curr_stat_dt))
from
(select
TicketID,
@y prev_stat,
@y:=Status curr_stat,
@x prev_stat_dt,
@x:=DateTime curr_stat_dt
from
table1
where
Status in ('Open' , 'Closed')) t
where
prev_stat = 'Open' and curr_stat = 'Closed';