[英]Date difference between two records in same table
I have a table ( job_logs
) with the following records: id
, job_id
, user_id
, status
, created_at
, job_type
. 我有一个包含以下记录的表(
job_logs
): id
, job_id
, user_id
, status
, created_at
, job_type
。
Each time a job starts to run a record is written in the job_log
table with status='started'
. 每次作业开始运行时,
job_log
以status='started'
将记录写入job_log
表中。 When a job finish running another record is added to the table with status='completed'
. 当作业完成运行时,另一个记录将以
status='completed'
添加到表中。
Both records has the same user_id
, job_type
and job_id (which is determined by the process running the job - unique to these 2 records). 这两个记录具有相同的
user_id
, job_type
和job_id (由运行作业的进程确定-这两个记录是唯一的)。
I want a query that will return all these records pairs in the table (ordered by id desc) but the tricky part is that I want to add to the record with the 'completed' status the time it took the job to run ( completed.created_at - started.created_at
). 我想要一个查询,该查询将返回表中的所有这些记录对(按id desc排序),但棘手的部分是我想将作业运行的时间(
completed.created_at - started.created_at
添加到“已完成”状态的记录中completed.created_at - started.created_at
)。
How can I do that? 我怎样才能做到这一点?
SELECT j1.job_id AS job_id, (j2.created_at - j1.created_at) AS time_run
FROM job_logs j1 INNER JOIN job_logs j2 ON (j1.job_id = j2.job_id)
WHERE j1.status = 'started' AND j2.status = 'completed'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.