gpt4 book ai didi

MySql - 计算多行的时间差

转载 作者:行者123 更新时间:2023-11-29 11:19:34 29 4
gpt4 key购买 nike

我有一个包含制造装配数据的表,包括时间戳。我试图确定“作业”开始之间的平均间隔(以分钟为单位)。

返回 ID 和时间的查询如下所示:

select job_id, job_started from JobTable where job_started >= '2016-07-01' and job_started <= '2016-07-31';

我正在寻找每行之间的时间差的输出:

1518211413

最佳答案

获取平均间隔(以秒为单位):

select (to_seconds(max(job_started)) - to_seconds(min(job_started))) / (count(*) - 1) as average_interval_seconds
from JobTable
where date(job_started) >= '2016-07-01'
and date(job_started) <= '2016-07-31'
;

获取所有间隔(以秒为单位):

select to_seconds((
select t2.job_started
from JobTable t2
where t2.job_started > t1.job_started
and date(t2.job_started) <= '2016-07-31'
limit 1
)) - to_seconds(t1.job_started) as interval_seconds
from JobTable t1
where date(t1.job_started) >= '2016-07-01'
and date(t1.job_started) <= '2016-07-31'
and t1.job_started <> (
select job_started
from JobTable
where date(job_started) <= '2016-07-31'
order by job_started desc
limit 1
)
;

http://sqlfiddle.com/#!9/1f8dc3/2

关于MySql - 计算多行的时间差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39155968/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com