我有如下数据(视图):添加附加行
Declare @YourTable table ([Event] varchar(100),[Start] DateTime,[End] DateTime, [Tag] varchar(25))
Insert Into @YourTable values
('10PIC700422.PV 10-PSV-700073A 10-PSV-700073B','9/9/16 10:44:05.000','9/9/16 10:48:08.000','Big'),
('10PIC700422.PV 10-PSV-700073A 10-PSV-700073B','9/9/16 10:44:10.000','9/9/16 10:49:40.000','Big'),
('10PIC700422.PV 10-PSV-700073A 10-PSV-700073B','9/9/16 10:50:03.000','9/9/16 10:51:04.000','Small'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 12:51:07.000','4/4/16 13:58:09.000','Big'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 14:04:04.000','4/4/16 14:29:00.000','Small'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 14:51:02.000','4/4/16 14:58:00.000','Big'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 15:04:06.000','4/4/16 15:29:08.000','Small'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4-4-16 15:45:08.000','4-4-16 15:55:09.000','Big'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4-4-16 16:22:08.000','4-4-16 16:40:09.000','Big'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 16:50:04.000','4/4/16 16:55:00.000','Small'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 17:05:02.000','4/4/16 17:20:00.000','Big'),
('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 17:22:06.000','4/4/16 17:29:08.000','Small'),
('11PIC41010.PV 11-PSV-401002W 11-PSV-401002D','4/4/16 16:04:01.000','4/4/16 16:45:00.000','Big');
我用下面的查询通过事件和秩序由开始得到结果我需要,组,变大变小后大时小云:
Select [Event]
,[Start]
,[End]
,[Tag]
,[Tag_new] = case when Tag='Big' and 'Small' = Lead(Tag,1,Tag) over (Partition By Event Order By Start) then 'Small' else tag end
From @YourTable
Event Start End Tag Tag_new
10PIC700422.PV 10-PSV-700073A 10-PSV-700073B 2016-09-09 10:44:05.000 2016-09-09 10:48:08.000 Big Big
10PIC700422.PV 10-PSV-700073A 10-PSV-700073B 2016-09-09 10:44:10.000 2016-09-09 10:49:40.000 Big Small
10PIC700422.PV 10-PSV-700073A 10-PSV-700073B 2016-09-09 10:50:03.000 2016-09-09 10:51:04.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 12:51:07.000 2016-04-04 13:58:09.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 14:04:04.000 2016-04-04 14:29:00.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 14:51:02.000 2016-04-04 14:58:00.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 15:04:06.000 2016-04-04 15:29:08.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 15:45:08.000 2016-04-04 15:55:09.000 Big Big
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 16:22:08.000 2016-04-04 16:40:09.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 16:50:04.000 2016-04-04 16:55:00.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 17:05:02.000 2016-04-04 17:20:00.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 17:22:06.000 2016-04-04 17:29:08.000 Small Small
11PIC41010.PV 11-PSV-401002W 11-PSV-401002D 2016-04-04 16:04:01.000 2016-04-04 16:45:00.000 Big Big
我需要申请多一个例外,每当序列标签栏下方出现一组,应该经过小前大与开始时间,结束时间和Tag_new添加相应的aditional的行,开始时间应该是1秒比之前的日期晚,endtime和endtime比starttime晚1秒d Tag_new是 “坏”:
Small
Big
Small
我想获得像下面的东西:
Event Start End Tag Tag_new
10PIC700422.PV 10-PSV-700073A 10-PSV-700073B 2016-09-09 10:44:05.000 2016-09-09 10:48:08.000 Big Big
10PIC700422.PV 10-PSV-700073A 10-PSV-700073B 2016-09-09 10:44:10.000 2016-09-09 10:49:40.000 Big Small
10PIC700422.PV 10-PSV-700073A 10-PSV-700073B 2016-09-09 10:50:03.000 2016-09-09 10:51:04.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 12:51:07.000 2016-04-04 13:58:09.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 14:04:04.000 2016-04-04 14:29:00.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 14:29:01.000 2016-04-04 14:29:02.000 Bad Bad
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 14:51:02.000 2016-04-04 14:58:00.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 15:04:06.000 2016-04-04 15:29:08.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 15:45:08.000 2016-04-04 15:55:09.000 Big Big
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 16:22:08.000 2016-04-04 16:40:09.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 16:50:04.000 2016-04-04 16:55:00.000 Small Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 16:55:01.000 2016-04-04 16:55:02.000 Bad Bad
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 17:05:02.000 2016-04-04 17:20:00.000 Big Small
11PIC41010.PV 11-PSV-401002A 11-PSV-401002B 2016-04-04 17:22:06.000 2016-04-04 17:29:08.000 Small Small
11PIC41010.PV 11-PSV-401002W 11-PSV-401002D 2016-04-04 16:04:01.000 2016-04-04 16:45:00.000 Big Big
我投了这个票,因为问题相当好。有示例代码来复制'n'paste,自己的努力和一个非常明确的解释。整个方法闻起来有点尴尬 - 复杂... SQL Server可能不是最好的工具。但是这个问题本身并不值得“因为不清楚”而得到“不公平”,也不值得投票。只有我2美分... – Shnugo
感谢Shnugo为其投票 –
为什么你要转贴这个问题,这里回答了这个问题:http://stackoverflow.com/questions/40026002/group-and-order-with-additional-exception/40027420#40027420? – iamdave