【问题标题】:Using Lead/Lag with additional exception使用领先/落后有额外的例外
【发布时间】:2017-02-22 06:37:57
【问题描述】:

我有下表:

 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

唯一的事情是,每当下面的序列出现在组中的标记列中时,我需要添加一个异常,它应该相应地在 starttime 和 endtime 之间添加额外的行,并且 starttime 比之前的 endtime 和 endtime 多 1 分钟比 starttime 多 1 分钟,并且 Tag_new 是“bad”:

  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

【问题讨论】:

  • 这条线应该是4-4-16 14:51 4-4-16 14:58 Big Bigsmall 吗?

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


【解决方案1】:

你可以尝试使用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

【讨论】:

  • sagi,非常感谢你的代码,看来是我需要的,我会用真实数据测试一下。
  • sagi,对不起,我发现我需要与我最初报告的不同的例外。查看我的更新。
猜你喜欢
  • 1970-01-01
  • 2018-02-10
  • 2013-08-30
  • 2022-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
相关资源
最近更新 更多