我有一个包含以下数据的表
ReferenceNo CommentDate StepName Originator StartTime
1 CHQRCL/0083/2013/FEB 2013-02-25 15:20:07.000 Initiate Process P8Admin 2013-02-25 15:20:07.000
1 CHQRCL/0083/2013/FEB 2013-02-25 15:20:44.000 Finance Controller P8Admin 2013-02-25 15:20:07.000
1 CHQRCL/0083/2013/FEB 2013-02-25 15:21:52.000 Originator P8Admin 2013-02-25 15:20:07.000
1 CHQRCL/0083/2013/FEB 2013-02-25 15:23:26.000 Finance Controller P8Admin 2013-02-25 15:20:07.000
1 CHQRCL/0083/2013/FEB 2013-02-25 15:23:55.000 Finance Officer P8Admin 2013-02-25 15:20:07.000
2 CHQRCL/0089/2013/FEB 2013-02-25 15:43:48.000 Initiate Process P8Admin 2013-02-25 15:43:48.000
2 CHQRCL/0089/2013/FEB 2013-02-25 15:43:59.000 Finance Controller P8Admin 2013-02-25 15:43:48.000
2 CHQRCL/0089/2013/FEB 2013-02-25 15:44:11.000 Asset GM P8Admin 2013-02-25 15:43:48.000
3 CHQRCL/0093/2013/MAR 2013-03-06 16:19:05.000 Initiate Process P8Admin 2013-03-06 16:19:05.000
3 CHQRCL/0093/2013/MAR 2013-03-06 16:21:57.000 Finance Controller P8Admin 2013-03-06 16:19:05.000
3 CHQRCL/0093/2013/MAR 2013-03-06 16:23:55.000 Asset GM P8Admin 2013-03-06 16:19:05.000
3 CHQRCL/0093/2013/MAR 2013-03-06 16:24:09.000 CFO P8Admin 2013-03-06 16:19:05.000
3 CHQRCL/0093/2013/MAR 2013-03-06 16:24:19.000 CEO P8Admin 2013-03-06 16:19:05.000
3 CHQRCL/0093/2013/MAR 2013-03-06 16:24:40.000 Finance Officer P8Admin 2013-03-06 16:19:05.000
4 CHQRCL/0094/2013/MAR 2013-03-06 16:58:47.000 Initiate Process P8Admin 2013-03-06 16:58:47.000
4 CHQRCL/0094/2013/MAR 2013-03-06 17:00:45.000 Finance Controller P8Admin 2013-03-06 16:58:47.000
4 CHQRCL/0094/2013/MAR 2013-03-06 17:00:54.000 Asset GM P8Admin 2013-03-06 16:58:47.000
4 CHQRCL/0094/2013/MAR 2013-03-06 17:04:48.000 CFO P8Admin 2013-03-06 16:58:47.000
4 CHQRCL/0094/2013/MAR 2013-03-06 17:05:06.000 CEO P8Admin 2013-03-06 16:58:47.000
4 CHQRCL/0094/2013/MAR 2013-03-06 17:05:35.000 Finance Officer P8Admin 2013-03-06 16:58:47.000
5 CHQRCL/0108/MAR/2013 2013-03-10 08:58:22.000 Initiate Process Mohamed Ubaid 2013-03-10 08:58:22.000
5 CHQRCL/0108/MAR/2013 2013-03-10 09:06:23.000 Finance Controller Mohamed Ubaid 2013-03-10 08:58:22.000
5 CHQRCL/0108/MAR/2013 2013-03-10 09:07:06.000 Finance Officer Mohamed Ubaid 2013-03-10 08:58:22.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:26:10.000 Initiate Process Allan C Fampulme 2013-03-10 09:26:10.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:32:34.000 Finance Controller Allan C Fampulme 2013-03-10 09:26:10.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:33:22.000 Asset GM Allan C Fampulme 2013-03-10 09:26:10.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:35:39.000 Originator Allan C Fampulme 2013-03-10 09:26:10.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:36:06.000 Asset GM Allan C Fampulme 2013-03-10 09:26:10.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:36:20.000 CFO Allan C Fampulme 2013-03-10 09:26:10.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:36:41.000 CEO Allan C Fampulme 2013-03-10 09:26:10.000
6 CHQRCL/0110/MAR/2013 2013-03-10 09:37:34.000 Finance Officer Allan C Fampulme 2013-03-10 09:26:10.000
首先,我希望每个参考编号的每个步骤(也就是财务总监的参考编号为1,需要37秒)。我想计算平均值(以相同的财务控制器步骤为例; 1 refNo时间为7秒+在1refNo中再次为94秒+在2refNo +中为11秒,依此类推6分)
答案 0 :(得分:0)
我使用以下查询解决了我的问题,
与CTE AS(
SELECT ROW_NUMBER()OVER(PARTITION BY ReferenceNo ORDER BY CommentDate)As RowNum,*
FROM FNCUSTOM.dbo.WorkflowHistory T
)
SELECT SELECTNo,StepName,CommentDate,DiffDays = DATEDIFF(s,(SELECT CommentDate FROM CTE c2 WHERE c2.ReferenceNo = CTE.ReferenceNo AND c2.RowNum = CTE.RowNum-1),CommentDate)/ 60.0 / 60.0 / 24.0
FROM CTE