0

一项活动中有多个延迟。可以将延迟放置在边界处,并且许多延迟可以存在于一个 Long 活动中。我们需要优先考虑延迟记录,因此我们应该忽略延迟记录中出现的任何活动记录

我在一个表中有两组记录,即特定机器在特定 shiftdate 和 shift 中的 ACTIVITY 和 DELAY。在某些轮班日期和轮班期间,活动和延迟之间发生重叠。活动记录之间发生的延迟记录应优先考虑,活动记录应围绕延迟记录相应拆分,从而消除活动和延迟之间的重叠

我应该使用什么方法来根据延迟分解活动记录,并将延迟优先于活动记录。

在此处输入图像描述

创建表的代码如下

CREATE TABLE [dbo].[Activity_Delays](
    [Type] [nvarchar](15) NOT NULL,
    [Shiftdate] [datetime] NOT NULL,
    [Shift#] [int] NOT NULL,
    [MachineID] [nvarchar](10) NOT NULL,
    [StartTime] [datetime] NOT NULL,
    [EndTime] [datetime] NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13B00000000 AS DateTime), 1, N'780', CAST(0x0000A13900735B40 AS DateTime), CAST(0x0000A1390083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13B00000000 AS DateTime), 1, N'780', CAST(0x0000A1390083D600 AS DateTime), CAST(0x0000A139009450C0 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A13900761A60 AS DateTime), CAST(0x0000A1390078D980 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A139007B98A0 AS DateTime), CAST(0x0000A139007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A13900815D30 AS DateTime), CAST(0x0000A1390084A8F0 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A1390087F4B0 AS DateTime), CAST(0x0000A139008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A0071B560 AS DateTime), CAST(0x0000A13A00761A60 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A007B98A0 AS DateTime), CAST(0x0000A13A007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A0087F4B0 AS DateTime), CAST(0x0000A13A008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A008D72F0 AS DateTime), CAST(0x0000A13A008FA570 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A13900761A60 AS DateTime), CAST(0x0000A1390078D980 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A139007B98A0 AS DateTime), CAST(0x0000A139007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A13900815D30 AS DateTime), CAST(0x0000A1390084A8F0 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A1390087F4B0 AS DateTime), CAST(0x0000A139008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A0071B560 AS DateTime), CAST(0x0000A13A00761A60 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A007B98A0 AS DateTime), CAST(0x0000A13A007FB750 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A0087F4B0 AS DateTime), CAST(0x0000A13A008C1360 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Delay', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A008D72F0 AS DateTime), CAST(0x0000A13A008FA570 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A13900735B40 AS DateTime), CAST(0x0000A1390083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'780', CAST(0x0000A1390083D600 AS DateTime), CAST(0x0000A139008ED280 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A00735B40 AS DateTime), CAST(0x0000A13A0083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'780', CAST(0x0000A13A0083D600 AS DateTime), CAST(0x0000A13A008ED280 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A13900735B40 AS DateTime), CAST(0x0000A1390083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13900000000 AS DateTime), 1, N'781', CAST(0x0000A1390083D600 AS DateTime), CAST(0x0000A139008ED280 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A00735B40 AS DateTime), CAST(0x0000A13A0083D600 AS DateTime))
INSERT [dbo].[Activity_Delays] ([Type], [Shiftdate], [Shift#], [MachineID], [StartTime], [EndTime]) VALUES (N'Activity', CAST(0x0000A13A00000000 AS DateTime), 1, N'781', CAST(0x0000A13A0083D600 AS DateTime), CAST(0x0000A13A008ED280 AS DateTime))

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

0

这是您的示例输出的查询:

select *, Activity_Delays.StartTime SortStartTime
from Activity_Delays
where 
   Type = 'Delay'
union all
select distinct Activities.*, Delays.StartTime SortStartTime
from
 Activity_Delays Delays
 inner join Activity_Delays Activities on Activities.ShiftDate = Delays.ShiftDate and
                                      Activities.MachineID = Delays.MachineID and
                                      ((Delays.EndTime between Activities.StartTime and Activities.EndTime) or
                                      (Delays.StartTime between Activities.StartTime and Activities.EndTime))
where
  Activities.Type = 'Activity' and
  Delays.Type = 'Delay'
order by MachineID, ShiftDate, SortStartTime, 1 desc

这里也是一个工作输出

于 2013-06-14T21:39:11.263 回答