从下一行开始日期开始更新结束-数百万行

时间:2019-12-22 09:22:00

标签: sql sql-server tsql

如果每个访问ID的下一个StartDate值都不匹配,如何更新EndDate值?请记住,该表包含400万个个人VisitID和总计超过1000万条记录。

例如,对于ID 3362144和VisitID 1159828,结束日期应为2019-03-26 10:29:54.297而不是2019-03-25 23:29:54.43

我有以下给出正确结果的SQL,但我想知道其他人如何使用update子句在1000万行中做到这一点?

SELECT *,
  ISNULL(
        LEAD( StartDate, 1 ) 
            OVER( PARTITION BY VisitID ORDER BY StartDate ) 
            ,
        EndDate) AS NewEndDate
from testdata2

测试数据

CREATE TABLE [dbo].[testdata2](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [VisitId] [int] NOT NULL,
    [StartDate] [datetime] NULL,
    [EndDate] [datetime] NULL
)

GO
SET IDENTITY_INSERT [dbo].[testdata2] ON 
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362144, 1159828, CAST(N'2019-03-26T10:28:38.663' AS DateTime), CAST(N'2019-03-25T23:29:54.243' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362147, 1159828, CAST(N'2019-03-26T10:29:54.297' AS DateTime), CAST(N'2019-03-25T23:30:51.300' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362148, 1159828, CAST(N'2019-03-26T10:30:51.350' AS DateTime), CAST(N'2019-03-25T23:34:51.240' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362150, 1159828, CAST(N'2019-03-26T10:34:51.287' AS DateTime), CAST(N'2019-03-25T23:37:10.980' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362152, 1159828, CAST(N'2019-03-26T10:37:11.030' AS DateTime), CAST(N'2019-03-25T23:37:29.167' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362154, 1159828, CAST(N'2019-03-26T10:37:29.213' AS DateTime), CAST(N'2019-03-25T23:37:42.407' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362155, 1159828, CAST(N'2019-03-26T10:37:42.453' AS DateTime), CAST(N'2019-03-25T23:46:45.230' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362170, 1159828, CAST(N'2019-03-26T10:46:45.500' AS DateTime), CAST(N'2019-03-25T23:51:26.163' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362175, 1159828, CAST(N'2019-03-26T10:51:26.220' AS DateTime), CAST(N'2019-03-26T00:19:34.670' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3362195, 1159828, CAST(N'2019-03-26T11:19:34.723' AS DateTime), CAST(N'2019-03-26T11:21:19.270' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3449893, 1196705, CAST(N'2019-12-18T13:43:38.183' AS DateTime), CAST(N'2019-12-18T02:44:17.273' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3449895, 1196705, CAST(N'2019-12-18T13:44:16.787' AS DateTime), CAST(N'2019-12-18T02:51:07.277' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3449900, 1196705, CAST(N'2019-12-18T13:51:06.843' AS DateTime), CAST(N'2019-12-18T14:01:42.687' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3449982, 1196741, CAST(N'2019-12-18T15:46:07.727' AS DateTime), CAST(N'2019-12-18T04:47:04.317' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3449989, 1196741, CAST(N'2019-12-18T15:47:03.820' AS DateTime), CAST(N'2019-12-18T04:55:20.937' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3449996, 1196741, CAST(N'2019-12-18T15:55:20.450' AS DateTime), CAST(N'2019-12-18T16:24:08.480' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3450022, 1196754, CAST(N'2019-12-18T16:32:39.527' AS DateTime), CAST(N'2019-12-18T05:33:38.833' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3450024, 1196754, CAST(N'2019-12-18T16:33:38.350' AS DateTime), CAST(N'2019-12-18T06:20:30.327' AS DateTime))
GO
INSERT [dbo].[testdata2] ([Id], [VisitId], [StartDate], [EndDate]) VALUES (3450038, 1196754, CAST(N'2019-12-18T17:20:29.843' AS DateTime), CAST(N'2019-12-18T17:29:42.933' AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[testdata2] OFF
GO

1 个答案:

答案 0 :(得分:2)

如果要更新表,可以使用一个简单的可更新的公用表表达式来做到这一点:

WITH CTE AS
(
    SELECT  [EndDate],
            ISNULL(
                LEAD( StartDate, 1 ) 
                OVER( PARTITION BY VisitID ORDER BY StartDate ) 
            , [EndDate]) AS NewEndDate
    FROM [dbo].[testdata2]
)

UPDATE CTE
SET [EndDate] = [NewEndDate]
WHERE [EndDate] <> [NewEndDate]