使用潜在客户/滞后额外例外

时间:2016-10-13 08:27:07

标签: sql-server tsql sql-server-2012

我有以下表格:

 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','9/9/16 10:49','Big'),
 ('10PIC700422.PV 10-PSV-700073A 10-PSV-700073B','9/9/16 10:50','9/9/16 10:51','Small'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 12:51','4/4/16 13:58','Big'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 14:04','4/4/16 14:29','Small'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 14:51','4/4/16 14:58','Big'),
 ('11PIC41010.PV 11-PSV-401002A 11-PSV-401002B','4/4/16 15:04','4/4/16 15:29','Small'),
 ('11PIC41010.PV 11-PSV-401002W 11-PSV-401002D','4/4/16 16:04','4/4/16 16:45','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   9-9-16 10:44  9-9-16 10:49    Big    Small
   10PIC700422.PV 10-PSV-700073A 10-PSV-700073B   9-9-16 10:50  9-9-16 10:51    Small  Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 12:51  4-4-16 13:58    Big    Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 14:04  4-4-16 14:29    Small  Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 14:51  4-4-16 14:58    Big    Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 15:04  4-4-16 15:29    Small  Small
   11PIC41010.PV 11-PSV-401002W 11-PSV-401002D    4-4-16 16:04  4-4-16 16:45    Big    Big

唯一的事情是我需要在组中的Tag列中出现以下序列时添加一个异常,它应该相应地在start和endtime之间添加小到大之间的aditional行,并且starttime比之前的endtime和endtime多1分钟比starttime多1分钟,Tag_new是“坏”:

  small
  big
  small

我希望得到以下结果:

   Event                                          Start         End             Tag    Tag_new
   10PIC700422.PV 10-PSV-700073A 10-PSV-700073B   9-9-16 10:44  9-9-16 10:49    Big    Small
   10PIC700422.PV 10-PSV-700073A 10-PSV-700073B   9-9-16 10:50  9-9-16 10:51    Small  Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 12:51  4-4-16 13:58    Big    Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 14:04  4-4-16 14:29    Small  Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 14:30  4-4-16 14:31    Bad    Bad
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 14:51  4-4-16 14:58    Big    Small
   11PIC41010.PV 11-PSV-401002A 11-PSV-401002B    4-4-16 15:04  4-4-16 15:29    Small  Small
   11PIC41010.PV 11-PSV-401002W 11-PSV-401002D    4-4-16 16:04  4-4-16 16:45    Big    Big

1 个答案:

答案 0 :(得分:0)

您可以尝试使用LEAD()两次,选择下一个val和最后一个val,然后在所有条件下使用CASE EXPRESSION

SELECT [Event],[Start],[End],[Tag],
       CASE WHEN t.next_one = 'Small' and t.Last_one = 'Small' And t.Tag = 'Big' then 'Big'
            WHEN t.tag = 'Big' and t.next_one = 'Small' THEN 'Small'
            ELSE t.tag
       END as new_tag
FROM(
     Select [Event]
       ,[Start]
       ,[End] 
       ,[Tag]
       Lead(Tag,1,Tag) over (Partition By Event Order By Start) as next_one,
       Lead(Tag,1,Tag) over (Partition By Event Order By Start DESC) as last_one,
    From  @YourTable) t