Sql server 添加其他行

Sql server 添加其他行,sql-server,tsql,sql-server-2012,Sql Server,Tsql,Sql Server 2012,我有以下数据(视图): 我使用以下查询来获得我需要的结果,按事件分组,按开始排序,并在“小”在“大”之后变为“小”: 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 @Your

我有以下数据(视图):

我使用以下查询来获得我需要的结果,按事件分组,按开始排序,并在“小”在“大”之后变为“小”:

 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
我需要再应用一个例外,每当下面的序列出现在组中的标记列中时,它应该在小的后面和大的前面添加传统的行,并相应地使用starttime、endtime和Tag_new,starttime应该比上一个日期endtime晚1秒,endtime应该比starttime晚1秒,Tag_new是“坏的”:

我想得到如下信息:

                                       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

此设计有异味,但您可以尝试以下方式:

您应该避免特定于区域性的日期时间文字

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');
--向查询中添加一个运行行号,并为所有原始行添加一个
0

WITH YourQueryEnhanced AS
(
    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
    ,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS SortInx --You should put a secure ORDER BY here!!!
    ,0 AS InnerSortInx
    From  @YourTable
)
--使用
LAG()
检测序列。重叠的错误序列有一个问题

,BadMarked AS
(
    SELECT *
        ,CASE WHEN LAG([Tag],1) OVER(Order By SortInx)='Small' 
                AND LAG([Tag],2) OVER(Order By SortInx)='Big' 
                AND LAG([Tag],3) OVER(Order By SortInx)='Small' THEN 1 ELSE 0 END AS BadSequence 

    FROM YourQueryEnhanced
)
--这将选择所有
original
行,并为标记为
BadSequence
的每一行添加
bad
行。SortInx设置为prev值,内部SortInx设置为
1
,因此
ORDER BY
bad
行推到正确的位置

    SELECT *
    FROM BadMarked

    UNION ALL 

    SELECT [Event],[Start],[End],'Bad','Bad',SortInx-1,1,2 
    FROM BadMarked 
    WHERE BadMarked.BadSequence=1
    ORDER BY SortInx,InnerSortInx
注意

这将在结果中增加一行错误,正如您在预期结果中所述

原因是你的第3行到第7行有小的。中间的
小的
属于这两个序列


您必须在检测到错误序列的地方使用更多的逻辑(更多地调用
LAG()
)来解决这个问题,或者将最后一个选择包装为一个
CTE
,然后再次使用
LAG()
删除您不想要的错误行。

@Shnugo:您真的很接近了。谢谢你的解决方案

WITH YourQueryEnhanced AS
(
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
,ROW_NUMBER() OVER(Partition By [Event] Order By Start) AS SortInx --You     should put a secure ORDER BY here!!!
,0 AS InnerSortInx
From  @YourTable
), BadMarked AS
(
 SELECT *
    ,CASE WHEN LAG([Tag],1) OVER(Order By SortInx)='Small' 
            AND LAG([Tag],2) OVER(Order By SortInx)='Big' 
            AND LAG([Tag],3) OVER(Order By SortInx)='Big' THEN 1 ELSE 0 END     AS BadSequence 
FROM YourQueryEnhanced 
) 
select * from
(
SELECT *
FROM BadMarked
UNION ALL 
    SELECT [Event],DATEADD(s, -1, [Start]) [Start],    [End],'Bad','Bad',SortInx-1,1,2 
FROM BadMarked 
WHERE BadMarked.BadSequence=1
) a order by [Event]
,[Start]

我投了赞成票,因为这个问题很好。这里有复制“n”粘贴的示例代码、自己的努力和非常清晰的解释。整个方法闻起来有点复杂。。。SQL Server可能不是实现这一点的最佳工具。但这个问题本身既不值得“因为不清楚而结束”,也不值得投反对票。只有我的2美分…谢谢Shnugo投票支持为什么你回复了这个问题,答案在这里:?对不起,iamdave,今天经过几次测试后,我发现将small改为small2不是我需要的,因为如果有小-大-大-小,它会给两组small2,这是错误的方向,正确的方法是在小的后面和大的前面添加行。Shnugo,谢谢你的代码,但这似乎不是我需要的。@JoeGreen,请详细说明。。。为什么不呢?好的,我将尝试处理额外的行(仍然不确定如何处理),在starttime和endtime中添加1秒怎么样?@JoeGreen try dateadd()
    SELECT *
    FROM BadMarked

    UNION ALL 

    SELECT [Event],[Start],[End],'Bad','Bad',SortInx-1,1,2 
    FROM BadMarked 
    WHERE BadMarked.BadSequence=1
    ORDER BY SortInx,InnerSortInx
WITH YourQueryEnhanced AS
(
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
,ROW_NUMBER() OVER(Partition By [Event] Order By Start) AS SortInx --You     should put a secure ORDER BY here!!!
,0 AS InnerSortInx
From  @YourTable
), BadMarked AS
(
 SELECT *
    ,CASE WHEN LAG([Tag],1) OVER(Order By SortInx)='Small' 
            AND LAG([Tag],2) OVER(Order By SortInx)='Big' 
            AND LAG([Tag],3) OVER(Order By SortInx)='Big' THEN 1 ELSE 0 END     AS BadSequence 
FROM YourQueryEnhanced 
) 
select * from
(
SELECT *
FROM BadMarked
UNION ALL 
    SELECT [Event],DATEADD(s, -1, [Start]) [Start],    [End],'Bad','Bad',SortInx-1,1,2 
FROM BadMarked 
WHERE BadMarked.BadSequence=1
) a order by [Event]
,[Start]