列值更改时重置并递增计数

时间:2019-02-25 14:08:29

标签: sql-server tsql ssms

希望有人可以提供帮助。我使用的是SQL Server 2008,兼容性级别为100,因此无法使用超前/滞后功能。

我要实现的目标是基于TRAN_ID和INTERACTION中的更改创建sub_task_id。所有数据均按Tran_ID,Task_ID和Interaction_DateTime排序

对于每个新的TRAN_ID,每个SUB_TASK都将从1开始,并且在详细说明新的“打开”交互之前,它将保持不变,并以1递增。

我已经用所需的结果预填充了SUB_TASK。不确定从哪里开始,将不胜感激。

-------------------------------------------------------------------------
TRAN_ID | TASK_ID | INTERACTION | INTERACTION_DATETIME    | SUB_TASK_ID
-------------------------------------------------------------------------
1234    | 1       | Open        | 2018-01-04 18:02:18     | 1
1234    | 1       | Close       | 2018-01-04 18:02:27     | 1
2234    | 11      | Open        | 2018-01-03 09:04:33     | 1
2234    | 11      | Close       | 2018-01-03 09:04:50     | 1
2234    | 11      | Open        | 2018-01-04 09:05:29     | 2
2234    | 11      | Edit        | 2018-01-04 09:06:42     | 2
2234    | 11      | Edit        | 2018-01-04 09:07:33     | 2
2234    | 11      | Merge       | 2018-01-04 09:09:21     | 2
2234    | 11      | Close       | 2018-01-04 09:13:50     | 2
2234    | 11      | Open        | 2018-01-05 11:14:34     | 3
2234    | 11      | Edit        | 2018-01-05 11:16:49     | 3
2234    | 11      | Edit        | 2018-01-05 11:21:21     | 3
2234    | 11      | Merge       | 2018-01-05 11:55:33     | 3
2234    | 11      | Close       | 2018-01-05 11:56:12     | 3
3242    | 13      | Open        | 2018-01-03 15:47:22     | 1
3242    | 13      | Close       | 2018-01-03 15:47:59     | 1
3242    | 13      | Open        | 2018-01-19 09:38:09     | 2
3242    | 13      | Edit        | 2018-01-19 09:39:10     | 2
3242    | 13      | Edit        | 2018-01-19 09:42:12     | 2
3242    | 13      | Close       | 2018-01-19 09:46:12     | 2

1 个答案:

答案 0 :(得分:0)

这是您的选择。

基本上是根据INTERACTION ='Open'所决定的,基于TRAN_ID的运行总计,因为您说过:“对于每个新的TRAN_ID,每个SUB_TASK将从1开始,并且在详细描述新的'Open'交互之前,它将保持不变,它将增加1“

挑战是直到SQL Server 2012+才支持累积总和,因此我们必须使用外部应用:

带有数据的示例临时表:

CREATE TABLE #TestData 
    (
        [TRAN_ID] INT
      , [TASK_ID] INT
      , [INTERACTION] NVARCHAR(10)
      , [INTERACTION_DATETIME] DATETIME
    );
INSERT INTO #TestData (
                          [TRAN_ID]
                        , [TASK_ID]
                        , [INTERACTION]
                        , [INTERACTION_DATETIME]
                      )
VALUES ( 1234, 1, 'Open', '2018-01-04 18:02:18' )
     , ( 1234, 1, 'Close', '2018-01-04 18:02:27' )
     , ( 2234, 11, 'Open', '2018-01-03 09:04:33' )
     , ( 2234, 11, 'Close', '2018-01-03 09:04:50' )
     , ( 2234, 11, 'Open', '2018-01-04 09:05:29' )
     , ( 2234, 11, 'Edit', '2018-01-04 09:06:42' )
     , ( 2234, 11, 'Edit', '2018-01-04 09:07:33' )
     , ( 2234, 11, 'Merge', '2018-01-04 09:09:21' )
     , ( 2234, 11, 'Close', '2018-01-04 09:13:50' )
     , ( 2234, 11, 'Open', '2018-01-05 11:14:34' )
     , ( 2234, 11, 'Edit', '2018-01-05 11:16:49' )
     , ( 2234, 11, 'Edit', '2018-01-05 11:21:21' )
     , ( 2234, 11, 'Merge', '2018-01-05 11:55:33' )
     , ( 2234, 11, 'Close', '2018-01-05 11:56:12' )
     , ( 3242, 13, 'Open', '2018-01-03 15:47:22' )
     , ( 3242, 13, 'Close', '2018-01-03 15:47:59' )
     , ( 3242, 13, 'Open', '2018-01-19 09:38:09' )
     , ( 3242, 13, 'Edit', '2018-01-19 09:39:10' )
     , ( 3242, 13, 'Edit', '2018-01-19 09:42:12' )
     , ( 3242, 13, 'Close', '2018-01-19 09:46:12' );

然后,我们可以使用外部应用。对于INTERACTION_DATETIME小于或等于的TASK_ID,我们将基于等于“打开”的INTERACTION求和。这应该在2008年生效:

SELECT      *
FROM        [#TestData] [a]
OUTER APPLY (
                SELECT SUM(   CASE WHEN [b].[INTERACTION] = 'Open' THEN 1
                                   ELSE 0
                              END
                          ) AS [SUB_TASK_ID]
                FROM   [#TestData] [b]
                WHERE  [b].[TRAN_ID] = [a].[TRAN_ID]
                       AND [b].[INTERACTION_DATETIME] <= [a].[INTERACTION_DATETIME]
            ) [s]
ORDER BY [a].[TRAN_ID], [a].[INTERACTION_DATETIME]

为您提供所需的结果:

TRAN_ID     TASK_ID     INTERACTION INTERACTION_DATETIME    SUB_TASK_ID
----------- ----------- ----------- ----------------------- -----------
1234        1           Open        2018-01-04 18:02:18.000 1
1234        1           Close       2018-01-04 18:02:27.000 1
2234        11          Open        2018-01-03 09:04:33.000 1
2234        11          Close       2018-01-03 09:04:50.000 1
2234        11          Open        2018-01-04 09:05:29.000 2
2234        11          Edit        2018-01-04 09:06:42.000 2
2234        11          Edit        2018-01-04 09:07:33.000 2
2234        11          Merge       2018-01-04 09:09:21.000 2
2234        11          Close       2018-01-04 09:13:50.000 2
2234        11          Open        2018-01-05 11:14:34.000 3
2234        11          Edit        2018-01-05 11:16:49.000 3
2234        11          Edit        2018-01-05 11:21:21.000 3
2234        11          Merge       2018-01-05 11:55:33.000 3
2234        11          Close       2018-01-05 11:56:12.000 3
3242        13          Open        2018-01-03 15:47:22.000 1
3242        13          Close       2018-01-03 15:47:59.000 1
3242        13          Open        2018-01-19 09:38:09.000 2
3242        13          Edit        2018-01-19 09:39:10.000 2
3242        13          Edit        2018-01-19 09:42:12.000 2
3242        13          Close       2018-01-19 09:46:12.000 2

对于2012+,您可以使用window功能完成所有这些操作。这就是您想要的样子。更干净,更轻松:

SELECT   *
       , SUM(   CASE WHEN [INTERACTION] = 'Open' THEN 1
                     ELSE 0
                END
            ) OVER ( PARTITION BY [TRAN_ID]
                     ORDER BY [TRAN_ID]
                            , [INTERACTION_DATETIME]
                   ) AS [SUB_TASK_ID]
FROM     [#TestData]
ORDER BY [TRAN_ID]
       , [TASK_ID]
       , [INTERACTION_DATETIME];