需要根据字段中文本的出现对行进行分组。说,当你看到 “ Started_Process1”应该启动其他聚合组。
需要的输出
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
感谢您的帮助
答案 0 :(得分:0)
这是解决问题的一种方法:
创建示例数据:
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
答案 1 :(得分:0)
您的数据存在的问题是流程可能重叠,但是我认为这是正确的逻辑:
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