如何2行计算找到特殊的2个单词?

问题描述 投票:0回答:3

我有一张桌子如下:

Date                              Msg

2019-04-11 10:14:02.773           AB123  <this is Succec bec next line is CD   
2019-04-11 10:14:02.647           CD123      
2019-04-11 10:11:03.670           AB123      
2019-04-11 10:11:03.500           CD123     
2019-04-10 09:53:39.743           AB123 <this is fail bec next line is not CD
2019-04-10 09:52:39.743           AB123
2019-04-10 09:53:39.743           CD123
2019-04-10 09:52:39.743           AB123 <this is fail bec next line is not CD
2019-04-10 09:52:39.743           AB123 <this is fail bec next line is NULL

我想找到Msg前2行AB和CD成功ORDER BY Date

如果Msg有AB继续出现将失败,直到CD 1设置

CREATE TABLE table1 (
  `Id` INTEGER,
  `Date` DATETIME,
  `Msg` VARCHAR(5)
);

INSERT INTO table1
  (`Id`, `Date`, `Msg`)
VALUES
  ('1', '2019-04-11 10:14:02.773', 'AB123'),
  ('2', '2019-04-11 10:14:02.647', 'CD123'),
  ('3', '2019-04-11 10:11:03.670', 'AB123'),
  ('4', '2019-04-11 10:11:03.500', 'CD123'),
  ('5', '2019-04-10 09:53:39.743', 'AB123'),
  ('6', '2019-04-09 09:53:39.587', 'AB123'),
  ('7', '2019-04-09 09:53:39.001', 'CD123'),
  ('8', '2019-04-08 07:53:39.587', 'AB123'), 
  ('9', '2019-04-07 08:53:39.111', 'AB123');

SELECT t.Id, t.Date, t.Msg 
FROM ( SELECT Id, Date, Msg, LEAD(Msg) OVER (ORDER BY Date DESC) AS NextMsg FROM table1 ) t 
WHERE t.Msg LIKE '%AB%' AND t.NextMsg LIKE '%AB%'  
ORDER BY t.Date DESC

我的预期产量是

Id  Date                Msg    Check
5   2019-04-10 09:53:40 AB123  fail
8   2019-04-08 07:53:40 AB123  fail
9  2019-04-07 08:53:39  AB123  fail

例子https://www.db-fiddle.com/f/9B2LzaAvwhFz1QRN53moQU/4

sql sql-server
3个回答
0
投票

如果我正确理解您的问题,并且您想使用当前行和后续行中的值过滤表,则使用LEAD()的下一步方法可能有所帮助:

输入

CREATE TABLE #Data (
    Id int,
    [Date] datetime,
    Msg varchar(5)
)
INSERT INTO #Data
    (Id, [Date], Msg)
VALUES
    (1, '2019-04-11T10:14:02.773', 'AB123'),      
    (2, '2019-04-11T10:14:02.647', 'CD123'),      
    (3, '2019-04-11T10:11:03.670', 'AB123'),      
    (4, '2019-04-11T10:11:03.500', 'CD123'),     
    (5, '2019-04-10T09:53:39.743', 'AB123'),
    (6, '2019-04-10T09:52:39.743', 'AB123')

声明

SELECT t.Id, t.[Date], t.[Msg]
FROM (
    SELECT 
        Id, 
        [Date], 
        Msg, 
        LEAD(Msg) OVER (ORDER BY [Date] DESC) AS NextMsg
    FROM #Data  
) t
WHERE t.Msg LIKE '%AB%' AND ((t.NextMsg NOT LIKE '%CD%') OR (t.NextMsg IS NULL))
ORDER BY t.[Date] DESC

产量

Id  Date                Msg
5   10/04/2019 09:53:39 AB123
6   10/04/2019 09:52:39 AB123

0
投票

如果我猜你想要的是这个,这将有效:

select a.*,case when regexp_like(a.Msg,'(.)*AB(.)*')=1 or
regexp_like(a.Msg,'(.)*CD(.)*')=1 then 'IsSuccess' 
when regexp_like(a.Msg,'(.)*EF(.)*')=1 then 'IsFail'
else null end  status from table1 a

检查https://www.db-fiddle.com/f/9B2LzaAvwhFz1QRN53moQU/0


0
投票

我怀疑你想找到在某个时间范围内没有“CD”值的“AB”值 - 比如1秒或1分钟。

如果是这样,请使用not exists

选择t。*从t,其中t.msg喜欢'AB%'而不存在(从t t2中选择1,其中t2.msg喜欢'CD%'和t2.date <t.date和t2.date> = dateadd(第二个) ,-10,t.date));

© www.soinside.com 2019 - 2024. All rights reserved.