MSSQL:计算时间多次启动和停止

时间:2018-05-30 05:45:24

标签: sql-server

是否可以在sql中创建:

拥有记录表:

    ID      LaborID     OrderNr      OrderStatusID  OrderStatusDate


    12990   3731573     OPT1814378          2       2018-05-28 09:35:30.123
    13105   230687389   OPT1814378          1       2018-05-29 10:32:14.850
    13106   230687389   OPT1814378          2       2018-05-29 10:52:14.403
    13123   230480202   OPT1814378          1       2018-05-29 13:18:05.233
    13130   230480202   OPT1814378          0       2018-05-29 13:29:17.360
    12837   3731573     OPT1814089          2       2018-05-25 20:28:24.817
    12906   10138504    OPT1814089          1       2018-05-26 10:41:18.680
    12909   10138504    OPT1814089          2       2018-05-26 10:57:40.733
    12913   10138504    OPT1814089          1       2018-05-26 11:41:48.387
    12920   10138504    OPT1814089          0       2018-05-26 12:15:48.590

,其中

OrderStatusID   
0 - End
1 - Begin
2 - pause

需要计算工作时间from begin to pause (1->2)from begin to end (1->0)。 我的问题是我必须遵守一些条件:

如果第一条记录为2,则忽略

工作始于1

但可以有更多停顿(1->2) 每次使用0

的最后一份工作结束记录

这种情况下的结果将是:

OPT1814378          230687389   00:20:00      
OPT1814378          230480202   00:11:12   
OPT1814089          10138504    00:16:12
OPT1814089          10138504    00:34:00

2 个答案:

答案 0 :(得分:2)

希望这不是那么难看。

gluster-heketi

; with cte as ( -- CTE for generating a sequence no select *, rn = row_number() over (partition by OrderNr order by OrderStatusDate) from @table ), cte2 as ( -- Clean up invalid any rows and regenerate new sequence no select ID, LaborID, OrderNr, OrderStatusID, OrderStatusDate, rn = row_number() over (partition by OrderNr order by OrderStatusDate) from cte where (rn = 1 and OrderStatusID = 1) or rn >= 2 ) select OrderNr, LaborID, convert(varchar(10), dateadd(second, datediff(second, min(OrderStatusDate), max(OrderStatusDate)), 0), 108) from cte2 group by OrderNr, LaborID, (rn - 1) / 2 将给出值0,0,1,1,2,2等,用于将行逐个分组。

答案 1 :(得分:0)

这个也有效

;WITH CTE
AS ( 
    SELECT row_number() OVER ( PARTITION BY ordernr ORDER BY id ) RN ,* FROM test_ti 
    )

,cte2
AS (
    SELECT *
    FROM cte c1
    WHERE NOT EXISTS (
            SELECT *
            FROM cte c2
            WHERE c1.id = c2.id
                AND c2.rn = 1
                AND c2.orderstatusid = 2
            )
    )
SELECT OrderNr
    ,LaborId
    ,TimeInterval
FROM ( SELECT DateDiff(MI, TIME, NxtTm) TimeInterval ,*
    FROM (
        SELECT * ,lead(TIME) OVER ( ORDER BY id ) NxtTm
        FROM cte2
        ) x
    WHERE orderstatusid <> 0
    ) y
WHERE orderstatusid = 1