select
ID,
min(case when 新状态 = 'A' then 成为新状态的时间 end) as 第一次成为A状态的时间,
min(case when 新状态 = 'B' then 成为新状态的时间 end) as 第一次成为B状态的时间
from
M
group by
ID
ID,
min(case when 新状态 = 'A' then 成为新状态的时间 end) as 第一次成为A状态的时间,
min(case when 新状态 = 'B' then 成为新状态的时间 end) as 第一次成为B状态的时间
from
M
group by
ID
from M group by id
where 成为新状态的时间 =
(select min(成为新状态的时间) from 日志表M
where id = a.id
and 新状态 = a.新状态)如上语句出来的临时表就是 id 新状态 第一次成为对应此新状态的时间 三列
要变成你要的结果,作个交叉报表就行了。