每次文本出现在日志中时,SQL行在不同的组中聚合

每次文本出现在日志中时,SQL行在不同的组中聚合,sql,oracle,process,Sql,Oracle,Process,需要根据字段中文本的外观对行进行分组。看到就说 已启动的\u进程1应启动不同的聚合组 所需产量 Prcs Count Min_Process_DT Process1 2 1/15/2019 1:15 Process5 5 1/15/2019 1:17 Process1 3 1/15/2019 1:21 Process5 3 1/15/2019 1:30 Process1 4 1/15/2019 1:25 样本数据集 S_ID M

需要根据字段中文本的外观对行进行分组。看到就说 已启动的\u进程1应启动不同的聚合组

所需产量

Prcs    Count   Min_Process_DT
Process1    2   1/15/2019 1:15
Process5    5   1/15/2019 1:17
Process1    3   1/15/2019 1:21
Process5    3   1/15/2019 1:30
Process1    4   1/15/2019 1:25
样本数据集

S_ID    Msg                 Process_DT  Stack_Trace
1   Started_Process1    1/15/2019 1:15  Something happened1
2   Ended_Process1      1/15/2019 1:16  Something happened2
3   Started_Process5    1/15/2019 1:17  Something happened3
4   InProgress_Process5 1/15/2019 1:18  Something happened4
5   InProgress_Process5 1/15/2019 1:19  Something happened5
6   InProgress_Process5 1/15/2019 1:20  Something happened6
7   Started_Process1    1/15/2019 1:21  Something happened7
8   Ended_Process5      1/15/2019 1:22  Something happened8
9   InProgress_Process1 1/15/2019 1:23  Something happened9
10  Ended_Process1      1/15/2019 1:24  Something happened10
11  Started_Process1    1/15/2019 1:25  Something happened11
12  InProgress_Process1 1/15/2019 1:26  Something happened12
13  InProgress_Process1 1/15/2019 1:27  Something happened13
14  InProgress_Process1 1/15/2019 1:28  Something happened14
16  Started_Process5    1/15/2019 1:30  Something happened16
17  InProgress_Process5 1/15/2019 1:31  Something happened17
18  Ended_Process5      1/15/2019 1:32  Something happened18

感谢您的帮助这里有一种解决方法:

创建示例数据:

Declare @table table (S_ID int, Msg nvarchar(100), Process_DT datetime, Stack_Trace nvarchar(100))
    Insert into @table Select 1,   'Started_Process1'       ,'1/15/2019 1:15',  'Something happened1'
    Insert into @table Select 2 ,  'Ended_Process1'         ,'1/15/2019 1:16', 'Something happened2'
    Insert into @table Select 3 ,  'Started_Process5'       ,'1/15/2019 1:17', 'Something happened3'
    Insert into @table Select 4 ,  'InProgress_Process5'    ,'1/15/2019 1:18', 'Something happened4'
    Insert into @table Select 5 ,  'InProgress_Process5'    ,'1/15/2019 1:19', 'Something happened5'
    Insert into @table Select 6 ,  'InProgress_Process5'    ,'1/15/2019 1:20', 'Something happened6'
    Insert into @table Select 7 ,  'Started_Process1'       ,'1/15/2019 1:21', 'Something happened7'
    Insert into @table Select 8 ,  'Ended_Process5'         ,'1/15/2019 1:22', 'Something happened8'
    Insert into @table Select 9 ,  'InProgress_Process1'    ,'1/15/2019 1:23', 'Something happened9'
    Insert into @table Select 10,  'Ended_Process1'         ,'1/15/2019 1:24', 'Something happened10'
    Insert into @table Select 11,  'Started_Process1'       ,'1/15/2019 1:25', 'Something happened11'
    Insert into @table Select 12,  'InProgress_Process1'    ,'1/15/2019 1:26', 'Something happened12'
    Insert into @table Select 13,  'InProgress_Process1'    ,'1/15/2019 1:27', 'Something happened13'
    Insert into @table Select 14,  'InProgress_Process1'    ,'1/15/2019 1:28', 'Something happened14'
    Insert into @table Select 16,  'Started_Process5'       ,'1/15/2019 1:30', 'Something happened16'
    Insert into @table Select 17,  'InProgress_Process5'    ,'1/15/2019 1:31', 'Something happened17'
    Insert into @table Select 18,  'Ended_Process5'         ,'1/15/2019 1:32', 'Something happened18'
和查询

Select    substring(t1.Msg, Charindex('_', t1.Msg, 1) + 1, len(t1.Msg) - Charindex('_', t1.Msg, 1)) [Prcs]
        , t2.count                                                                                  [Count]
        , t1.Process_DT                                                                             [Min_Process_DT]
        from    @table t1
               ,(select   ta.grp               [grp]
                         ,count(ta.Process_DT) [count]
                         ,min(ta.S_ID)         [S_ID]
                 from   (
                         select   sum(case when msg like 'Started_Process%' then 1 end) 
                                  over (order by substring(Msg, Charindex('_', Msg, 1) + 1, len(Msg) - Charindex('_', Msg, 1)), Process_DT) grp
                                 ,Process_DT
                                 ,S_ID
                                 ,substring(msg, charindex('_', Msg, 1) + 1, len(msg) - charindex('_', Msg, 1)) process
                         from    @table
                        ) ta
                group by ta.grp) t2
        where  Msg like ('Started%')
        and    t1.S_ID = t2.S_ID
        order by t1.Process_DT

您的数据存在的问题是过程可能重叠,但我认为这是正确的逻辑:

select p, min(dt) dt, count(1) cnt
  from (
    select id, p, dt, sum(g) over (partition by p order by id) grp 
      from (
        select s_id id, substr(msg, instr(msg, 'Process')) p, msg, process_dt dt, 
               case when msg like 'Started%' then 1 end g 
          from data))
  group by p, grp
  order by dt

您使用的是哪一版本的Oracle?您是否尝试过类似于通过表中的消息在分区上选择*;?这在甲骨文上行不通。哎呀,我没看到。。。。那么,可能有人会在甲骨文内部使用这个原则。谢谢你的回复。我向你们致敬,因为你们提出了正确的解决方案。逻辑确实提供了解决方案。我必须为10多个进程执行此操作。一个简单一点的SQL或过程解决方案是否可行?实际上,我们正在寻找的是启动一个计数器,并在每次字符串出现时更改它。但是,保持计数器不变,直到相同的字符串再次出现。谢谢您的回复。插入先前在perfect中创建的statment Rick。我们所要做的就是在每次出现预先确定的字符串时更改组。在示例中,需要为这两个已启动的进程1已启动的进程5重置分组,我还添加了一个列ev_ty,以帮助识别每个进程。但它可能会失败并重新启动。我必须为10多个进程执行此操作。越简单越好。一个简单一点的SQL或过程解决方案是否可行?实际上,我们正在寻找的是启动一个计数器,并在每次字符串出现时更改它。但随后保持计数器不变,直到再次出现相同的字符串。创建表PrcsS_ID int,ev_type nvarchar100,Msg nvarchar100,Process_DT datetime,Stack_Trace nvarchar100插入到Prcs选择1,'P1','Started_Process1','1/15/2019 1:15','Something happened1'插入到Prcs选择2,'P1','Ended_Process1','1/15/2019 1:16',“发生的事情2”插入Prcs选择3,“P5”,“开始处理5”,“2019年1月15日1:17”,“发生的事情3”插入Prcs选择4,“P5”,“正在处理5”,“2019年1月15日1:18”,“发生的事情4”插入Prcs选择5,“P5”,“正在处理5”,“2019年1月15日1:19”,“发生的事情5”插入Prcs选择6,“P5”,“正在进行的过程5”,“2019年1月15日1:20”,“发生的事情6”插入Prcs选择7,“P1”,“开始的过程1”,“2019年1月15日1:21”,“发生的事情7”插入Prcs选择8,“P5”,“结束的过程5”,“2019年1月15日1:22”,“发生的事情8”插入Prcs选择9,“P1”,“正在处理1”,“2019年1月15日1:23”,“发生的事情9”插入Prcs选择10,“P1”,“结束处理1”,“2019年1月15日1:24”,“发生的事情10”插入Prcs选择10,“P1”,“结束处理1”,“2019年1月15日1:24”,“发生的事情10”插入Prcs选择11,“P1”,“已开始处理1”,“2019年1月15日1:25”,“发生的事情11”插入Prcs选择12,“P1”,“正在处理1”,“2019年1月15日1:26”,“发生的事情12”插入Prcs选择13,“P1”,“正在处理1”,“2019年1月15日1:27”,“发生的事情13”插入Prcs选择14,“P1”,“进程1”,“2019年1月15日1:28”,“发生的事情14”