我有一张包含制造装配数据的表格,包括时间戳。我正在尝试确定“工作”开始之间的平均间隔(以分钟为单位)。
我的返回id和时间的查询如下:
select job_id, job_started from JobTable where job_started >= '2016-07-01' and job_started <= '2016-07-31';
我正在寻找输出,这将是每行之间的时间差异:
15 18 21 14 13
答案 0 :(得分:1)
获取平均间隔秒数:
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
)
;