2013-03-12 67 views
-1

我有一個包含像以下計算時間差和求和基於另一列

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秒的時間爲7秒+ 1秒的時間再次爲94秒+ 2秒的時間爲11秒+依此類推6)

+0

我曾與CTE AS( SELECT ROW_NUMBER()OVER(PARTITION BY ReferenceNo ORDER BY CommentDate)嘗試作爲ROWNUM ,* FROM ŤFNCUSTOM.dbo.WorkflowHistory \t --where stepname!= '啓動過程' ) SELECT DiffSec = DATEDIFF(S,CommentDate,(SELECT CommentDate FROM CTE C2 WHERE c2.ReferenceNo = CTE.ReferenceNo AND c2.RowNum = CTE.RowNum + 1)) ,ReferenceNo ,StepName ,CommentDate FROM CTE where referenceno ='CHQRCL/0157/MAR/2013' ORDER BY ReferenceNo,RowNum; – 2013-03-13 04:55:42

+0

其給出以下輸出 – 2013-03-13 04:56:16

+0

\t DiffSec ReferenceNo \t \t StepName CommentDate CHQRCL/0083/2013/FEB \t啓動過程\t 2013年2月25日15:20:07.000 CHQRCL/0083/2013/FEB \t金融控制器\t 2013年2月25日15:20:44.000 CHQRCL/0083/2013/FEB \t \t發起人15 2013年2月25日:21:52.000 CHQRCL/0083/2013/FEB \t財務總監\t 2013 -02-25 15:23:26.000 NULL財務主任\t 2013-02-25 15:23:55.000 – 2013-03-13 04:56:46

回答

0

我用這解決了我的問題,下面的查詢,

WITH AS CTE(
SELECT ROW_NUMBER()OVER(PARTITION BY ReferenceNo ORDER BY CommentDate)作爲ROWNUM,* FROM
ŤFNCUSTOM.dbo.WorkflowHistory


SELECT ReferenceNo,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