其中包括过程中的步骤以及每个步骤的状态。 对于已完成的游行,“完成”步骤为最后一步,其持续时间为0 没有“完成”阶段的过程-它仍然继续运行查询 我需要查询以在表中添加另一列,以计算过程中每个步骤的分钟数 谢谢您的帮助 哪种语法有效 添加了表创建和数据的语法:
Create table T_Step (
employee_ID INT
, Process_ID int
, Step_ID int
, Start_Date Datetime
, Step_Status varchar(30)
);
Insert into T_Step values
('1','1','1','2018-01-01 8:00' ,'Pending')
, ('1','1','2','2018-01-01 9:30' ,'InService')
, ('1','1','3','2018-01-01 9:45' ,'Done')
, ('2','2','1','2018-01-02 11:32','Pending')
, ('2','2','2','2018-01-02 11:40','InService')
, ('2','2','3','2018-01-02 12:20','Done')
;
谢谢
答案 0 :(得分:0)
使用LEFT JOIN
,然后为特定过程计算两步之间的时间差。
此查询适用于 MYSQL
select t1.employee_ID,t1.Process_ID,t1.Step_ID,t1.Start_Date,t1.Step_Status,
IFNULL(TIMESTAMPDIFF(MINUTE,t1.Start_Date,t2.Start_Date),0) As TimeInMinute
from T_Step t1
LEFT JOIN T_Step t2
ON t1.Process_ID=t2.Process_ID AND t1.Step_ID!=t2.Step_ID AND (t2.Step_ID-t1.Step_ID)=1
ORDER BY t1.Process_ID,t1.Step_ID;
输出
| employee_ID | Process_ID | Step_ID | Start_Date | Step_Status | TimeInMinute |
| ----------- | ---------- | ------- | ------------------- | ----------- | ------------ |
| 1 | 1 | 1 | 2018-01-01 08:00:00 | Pending | 90 |
| 1 | 1 | 2 | 2018-01-01 09:30:00 | InService | 15 |
| 1 | 1 | 3 | 2018-01-01 09:45:00 | Done | 0 |
| 2 | 2 | 1 | 2018-01-02 11:32:00 | Pending | 8 |
| 2 | 2 | 2 | 2018-01-02 11:40:00 | InService | 40 |
| 2 | 2 | 3 | 2018-01-02 12:20:00 | Done | 0 |
答案 1 :(得分:0)
declare @T_Step table (
employee_ID INT
, Process_ID int
, Step_ID int
, Start_Date Datetime
, Step_Status varchar(30)
);
Insert into @T_Step values
('1','1','1','2018-01-01 8:00' ,'Pending')
, ('1','1','2','2018-01-01 9:30' ,'InService')
, ('1','1','3','2018-01-01 9:45' ,'Done')
, ('2','2','1','2018-01-02 11:32','Pending')
, ('2','2','2','2018-01-02 11:40','InService')
, ('2','2','3','2018-01-02 12:20','Done')
;
with cte as (Select *, R=ROW_NUMBER()
over(partition by employee_ID order by employee_ID)
from @T_Step)
Select T1.employee_ID,T1.Process_ID,T1.Step_Status,t1.Start_Date,t2.Start_Date, DATEDIFF(SECOND,t2.Start_Date,t1.Start_Date) TimeTaken
from cte T1
left join cte T2 on T1.R = T2.R+1 and T1.employee_ID = T2.employee_ID
order by T1.employee_ID