希望有人可以提供帮助。我正在使用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
这是你的选择。
基本上由INTERACTION ='Open'决定的基于TRAN_ID的运行总计,因为您说:“每个SUB_TASK将从每个新的TRAN_ID开始,并且它将保持相同,直到详细说明新的'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等于'Open'进行求和。这应该在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+,您可以使用窗口功能完成所有这些操作。这就是你所拥有它的样子。更清洁,更容易:
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];