更改选择中的变量

时间:2018-05-17 15:42:53

标签: sql sql-server

我会说实话,我甚至不确定我想做的事情是否可行。通常我会在外部编写代码,但我需要在存储过程中使用它或找到另一种方法。

这实际上是一个时间表。我有一系列具有开始和结束时间的事件,其中一些事件发生冲突,但并不总是具有相同的开始/结束。在每组事件之间设置休息时段,将其分成块,每天有效地有4个块。我需要能够将所有冲突组合在一起以找到最早的开始时间和最新的结束时间,并将另一列参与者加起来。

我目前正在尝试使用滞后来查看事件是否属于前一个范围,这样可以正常工作,但我看不到将它们分组的方法。我希望使用一个仅在case语句的else部分中递增的变量,但这似乎是不行的。

declare @MyVar Integer
set @MyVar = 1

SELECT        RoomID, RoomNo, Date, DayOfYear, StartTime, EndTime, ActivityID,
case when 
LAG(StartTime,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityID) between starttime and endtime then LAG(ActivityID,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)
when LAG(endTime,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityID) between starttime and endtime then LAG(ActivityID,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityID)
else  0 end as ClashesWith
FROM            TTData
WHERE        (RoomNo = 'Room1')
ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID

我在这里完全叫错了树吗?说实话,咬掉我的舒适区。我可以很容易地写一个VBscript,但从来没有在TSQL中做过这样的事情。

编辑: 对不起,在我的脑海里,我很清楚,但回头看,我公然没有!

  • 我有一张每个事件的表格,无论冲突如何
  • 我想为每组冲突分配一个ID

希望这是数据的图像,目前为止最右边的列是我的输出: https://i.imgur.com/ASz8F9g.png

我想要的是最后一列在第1组中具有相同的数字,对于第2,3,4组具有不同的数字....如果没有找到冲突,则为0。

EDIT2:输入数据

CREATE TABLE mytable(
   RoomID INTEGER  NOT NULL PRIMARY KEY 
  ,RoomNo VARCHAR(6) NOT NULL
  ,DayOfYear INTEGER  NOT NULL
  ,StartTime DATETIME  NOT NULL
  ,EndTime DATETIME  NOT NULL
  ,ActivityID INTEGER  NOT NULL
  ,FIELD7 VARCHAR(11)
);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 09:00:00.000','2018-01-03 10:30:00.000',221456);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 09:00:00.000','2018-01-03 10:30:00.000',222129);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 09:00:00.000','2018-01-03 10:30:00.000',222251);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 09:00:00.000','2018-01-03 10:30:00.000',222389);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 09:00:00.000','2018-01-03 10:30:00.000',222527);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 10:45:00.000','2018-01-03 12:15:00.000',221491);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 10:45:00.000','2018-01-03 12:15:00.000',222160);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 10:45:00.000','2018-01-03 12:15:00.000',222286);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 10:45:00.000','2018-01-03 12:15:00.000',222424);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 10:45:00.000','2018-01-03 12:15:00.000',222562);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 10:45:00.000','2018-01-03 12:15:00.000',224183);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 13:45:00.000','2018-01-03 15:15:00.000',221921);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 13:45:00.000','2018-01-03 15:15:00.000',223167);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 15:30:00.000','2018-01-03 17:00:00.000',221956);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',3,'2018-01-03 15:30:00.000','2018-01-03 17:00:00.000',223202);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 09:00:00.000','2018-01-04 10:30:00.000',222664);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 09:00:00.000','2018-01-04 10:30:00.000',222800);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 09:00:00.000','2018-01-04 10:30:00.000',222968);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 09:00:00.000','2018-01-04 10:30:00.000',223098);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 10:45:00.000','2018-01-04 12:15:00.000',222698);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 10:45:00.000','2018-01-04 12:15:00.000',222831);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 10:45:00.000','2018-01-04 12:15:00.000',223004);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 10:45:00.000','2018-01-04 12:15:00.000',223132);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 13:45:00.000','2018-01-04 15:15:00.000',221507);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 13:45:00.000','2018-01-04 15:15:00.000',221782);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 13:45:00.000','2018-01-04 15:15:00.000',222320);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 13:45:00.000','2018-01-04 15:15:00.000',222458);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 13:45:00.000','2018-01-04 15:15:00.000',223426);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 15:30:00.000','2018-01-04 17:00:00.000',221541);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 15:30:00.000','2018-01-04 17:00:00.000',221816);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 15:30:00.000','2018-01-04 17:00:00.000',222354);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 15:30:00.000','2018-01-04 17:00:00.000',222492);
INSERT INTO mytable(RoomID,RoomNo,DayOfYear,StartTime,EndTime,ActivityID) VALUES (308,'P1C1',4,'2018-01-04 15:30:00.000','2018-01-04 17:00:00.000',223459);

1 个答案:

答案 0 :(得分:0)

实现了一个更简单的解决方案,至少对我自己的问题而言。我目前最多有4个冲突事件,所以我只使用了6个语句,这些语句滞后于1-6行。这意味着我可以在主数据的碰撞列中包含任何带有0的结果,并将具有ID的任何内容分组回初始事件。它并不完美,但它现在适合我的需求。

SELECT        RoomID, RoomNo, DayOfYear, StartTime, EndTime, ActivityOccurrenceID,
case 
when starttime between LAG(StartTime,6) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,6) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) or endTime between LAG(StartTime,6) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,6) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) then LAG(ActivityOccurrenceID,6) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)
when starttime between LAG(StartTime,5) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,5) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) or endTime between LAG(StartTime,5) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,5) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) then LAG(ActivityOccurrenceID,5) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)
when starttime between LAG(StartTime,4) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,4) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) or endTime between LAG(StartTime,4) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,4) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) then LAG(ActivityOccurrenceID,4) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)
when starttime between LAG(StartTime,3) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,3) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) or endTime between LAG(StartTime,3) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,3) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) then LAG(ActivityOccurrenceID,3) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)
when starttime between LAG(StartTime,2) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,2) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) or endTime between LAG(StartTime,2) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,2) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) then LAG(ActivityOccurrenceID,2) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)
when starttime between LAG(StartTime,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) or endTime between LAG(StartTime,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID) and DATEADD(mi, -1, LAG(endTime,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)) then LAG(ActivityOccurrenceID,1) OVER(ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID)
else  0 end as ClashesWith
FROM            myTable
ORDER BY RoomID, DayOfYear, StartTime, ActivityOccurrenceID