将“现在”行添加到持续时间计算

nos*_*vel 5 sql t-sql sql-server azure-sql-database

我有一个计算事件持续时间的查询。但是,它不包括仍然“开放”的事件的当前时间。我试图找出一种方法将其添加到下面。它在 Azure SQL 12.0.2000.8 上运行。根据示例,事件 18 和 19 已关闭(最后一条记录具有 StatusID<>1),因此我当前的计算是正确的。但是,事件 20 正在进行中(最后一条记录的 StatusId=1)并且需要计算上次更新和现在之间的时间。

结构:

CREATE TABLE [dbo].[IncidentActions](
    [Id] [INT] IDENTITY(1,1) NOT NULL,
    [IncidentId] [INT] NOT NULL,
    [ActionDate] [DATETIMEOFFSET](7) NOT NULL,
    [Description] [NVARCHAR](MAX) NOT NULL,
    [StatusId] [INT] NOT NULL
) ON [PRIMARY] 
GO
INSERT INTO [dbo].[IncidentActions] VALUES
( 51, 18, N'2020-03-10T13:39:27.8621563+00:00', N'This is a demo of the app ops incident management portal', 1 ), 
( 52, 18, N'2020-03-10T13:41:42.4306254+00:00', N'Superfast update we''re on it', 1 ), 
( 53, 18, N'2020-03-10T13:42:19.0766735+00:00', N'Found a workaround', 1 ), 
( 55, 18, N'2020-03-10T13:44:05.7958553+00:00', N'Suspending for now', 2 ), 
( 56, 18, N'2020-03-10T13:44:49.732564+00:00', N'No longer suspended', 1 ), 
( 57, 18, N'2020-03-10T13:45:09.8056202+00:00', N'All sorted', 3 ), 
( 58, 19, N'2020-03-11T14:47:05.6968653+00:00', N'This is just a test', 1 ), 
( 59, 19, N'2020-03-11T14:51:20.4522014+00:00', N'Found workaround and root cause, not yet fixed', 1 ), 
( 60, 19, N'2020-03-11T14:52:34.857061+00:00', N'Networking issues, updates suspended', 2 ), 
( 61, 19, N'2020-03-11T14:54:48.2262037+00:00', N'Network issue resolved, full functionality restored', 3 ), 
( 62, 20, N'2020-03-12T10:49:11.5595048+00:00', N'There is an ongoing issue', 1 ), 
( 63, 20, N'2020-03-12T11:29:37.9376805+00:00', N'This incident is ongoing....', 1 )
GO
CREATE TABLE [dbo].[IncidentStatuses](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Description] [nvarchar](500) NOT NULL
) ON [PRIMARY]
GO
INSERT INTO [dbo].[IncidentStatuses] VALUES
( 1, N'OPEN' ), ( 2, N'SUSPENDED' ), ( 3, N'CLOSED' )
GO
Run Code Online (Sandbox Code Playgroud)

询问:

WITH allActions
AS (SELECT IncidentId,
           ActionDate,
           IncidentStatuses.Description,
           ROW_NUMBER() OVER (PARTITION BY IncidentId ORDER BY ActionDate) AS rowNum
    FROM IncidentActions
    INNER JOIN dbo.IncidentStatuses ON IncidentStatuses.Id = IncidentActions.StatusId
    )
,actionPeriods
AS (SELECT firstAction.IncidentId,
          firstAction.Description StartStatus,
          secondAction.Description EndStatus,
          DATEDIFF(SECOND, firstAction.ActionDate, secondAction.ActionDate) SecondsElapsed
    FROM allActions firstAction
    INNER JOIN allActions secondAction ON firstAction.rowNum +1 = secondAction.rowNum --the next action
                                AND firstAction.IncidentId = secondAction.IncidentId --for the same incident
    )
SELECT 
    actionPeriods.IncidentId,
    SUM(CASE WHEN actionPeriods.StartStatus = 'OPEN' THEN actionPeriods.SecondsElapsed ELSE 0 END) SecondsActive,
    SUM(CASE WHEN actionPeriods.StartStatus <> 'OPEN' THEN actionPeriods.SecondsElapsed ELSE 0 END) SecondsInactive,
    SUM(actionPeriods.SecondsElapsed) SecondsElapsed
FROM actionPeriods
GROUP BY actionPeriods.IncidentId
GO
Run Code Online (Sandbox Code Playgroud)

小智 1

使用 CTE 是多余的。您可以使用 tsql 窗口函数。在这种情况下,滞后和/或超前功能。我根据您的表格添加了示例代码。

select 
IncidentId, 
StatusId,actiondate , 
lag(actiondate) over (partition by incidentid order by incidentid, actiondate) as previousrow,
coalesce(
lead(actiondate) over (partition by incidentid order by incidentid, actiondate),
case 
    when (max(actiondate) over (partition by incidentid order by incidentid) = actiondate) and (statusid = 3) then actiondate 
    when (max(actiondate) over (partition by incidentid order by incidentid) = actiondate) and (statusid = 1) then convert([DATETIMEOFFSET](7),getdate())
end
) as nextrow
from 
    dbo.IncidentActions 
order by 
    IncidentId, ActionDate 
Run Code Online (Sandbox Code Playgroud)