【发布时间】:2020-10-12 20:17:17
【问题描述】:
由于天线过于敏感,我有一些混乱的 RFID 数据。有一个物理过程可以跟踪 RFID 标签在一个周期中通过不同站点的移动。带有 RFID 标签的物品每天可以在循环中移动不止一次,但它不太可能在第一次读取后的两个小时内开始循环。
我正在尝试创建一个标志列来确定一个项目的新周期何时开始,或者返回一个项目经历了多少次周期的计数。
这里是一些示例数据:
CREATE TABLE [dbo].[samplerfiddata](
[Item] [nvarchar](50) NOT NULL,
[Station_Type] [nvarchar](50) NOT NULL,
[Station_Name] [nvarchar](50) NOT NULL,
[Timestamp] [datetime2](7) NOT NULL,
[Trying_to_Create_this_Flag_Column] [nvarchar](50) NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Decontamination', N'Decontamination', CAST(N'2020-10-10T06:30:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Decontamination', N'Decontamination', CAST(N'2020-10-11T14:30:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Washer', N'Washer', CAST(N'2020-10-11T14:45:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Decontamination', N'Decontamination', CAST(N'2020-10-11T15:15:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Other', N'Decontamination', CAST(N'2020-10-11T23:30:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Washer', N'Washer', CAST(N'2020-10-12T00:15:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Other', N'Decontamination', CAST(N'2020-10-12T00:45:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Other', N'Decontamination', CAST(N'2020-10-13T16:00:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Other', N'Decontamination', CAST(N'2020-10-13T16:30:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'A', N'Decontamination', N'Decontamination', CAST(N'2020-10-14T13:30:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'B', N'Other', N'Decontamination', CAST(N'2020-10-12T08:30:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'B', N'Decontamination', N'Decontamination', CAST(N'2020-10-12T14:30:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'B', N'Washer', N'Washer', CAST(N'2020-10-12T14:45:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'B', N'Decontamination', N'Decontamination', CAST(N'2020-10-12T15:15:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'B', N'Decontamination', N'Decontamination', CAST(N'2020-10-12T18:00:00.0000000' AS DateTime2), N'1')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'B', N'Washer', N'Washer', CAST(N'2020-10-13T18:15:00.0000000' AS DateTime2), N'0')
GO
INSERT [dbo].[RFID SQL Problem Sample Data] ([Item], [Station_Type], [Station_Name], [Timestamp], [Trying_to_Create_this_Flag_Column]) VALUES (N'B', N'Decontamination', N'Decontamination', CAST(N'2020-10-13T19:00:00.0000000' AS DateTime2), N'1')
GO
在此数据中,我们有两个不同的项目在循环中移动,并捕获了所有场景。新周期的业务逻辑定义为当一个项目被一个 RFID 天线(站)读取时,其中站类型 = 去污或站名称 = 去污,并且距离上一次循环的第一次读取已经超过两个小时.
项目 | Station_Type |站名 |时间戳 | Trying_to_Create_this_Flag_Column :--- | :---------------- | :---------------- | :---------------------------- | :-------------------------------- 一个 |去污 |去污 | 2020-10-10 06:30:00.0000000 | 1 一个 |去污 |去污 | 2020-10-11 14:30:00.0000000 | 1 一个 |垫圈 |垫圈 | 2020-10-11 14:45:00.0000000 | 0 一个 |去污 |去污 | 2020-10-11 15:15:00.0000000 | 0 一个 |其他 |去污 | 2020-10-11 23:30:00.0000000 | 1 一个 |垫圈 |垫圈 | 2020-10-12 00:15:00.0000000 | 0 一个 |其他 |去污 | 2020-10-12 00:45:00.0000000 | 0 一个 |其他 |去污 | 2020-10-13 16:00:00.0000000 | 1 一个 |其他 |去污 | 2020-10-13 16:30:00.0000000 | 0 一个 |去污 |去污 | 2020-10-14 13:30:00.0000000 | 1 乙|其他 |去污 | 2020-10-12 08:30:00.0000000 | 1 乙|去污 |去污 | 2020-10-12 14:30:00.0000000 | 1 乙|垫圈 |垫圈 | 2020-10-12 14:45:00.0000000 | 0 乙|去污 |去污 | 2020-10-12 15:15:00.0000000 | 0 乙|去污 |去污 | 2020-10-12 18:00:00.0000000 | 1 乙|垫圈 |垫圈 | 2020-10-13 18:15:00.0000000 | 0 乙|去污 |去污 | 2020-10-13 19:00:00.0000000 | 1最终,我试图返回如下所示的结果集:
项目 |循环 :--- | -----: 一个 | 5 乙| 4这怎么可能完成?
【问题讨论】:
标签: sql sql-server datetime recursive-query sql-server-2019