-- init schema & data
drop table if exists UserResult
create table UserResult(
dt datetime,
result int,
[user] varchar(20)
)
insert into UserResult
select * from (values ('20220917 12:00:00', 1,'a')
,('20220917 10:10:00',1,'a')
,('20220917 11:05:00',1,'a')
,('20220917 15:08:00',0,'a')
,('20220917 16:01:00',0,'a')
,('20220917 17:00:00',1,'a')
,('20220917 18:00:00',1,'a')
) as temp(dt, result, [user])
-- result here
;with temp as(
select result,
dt,
[user],
Row_Number() over(order by dt) as rid,
ROW_NUMBER() OVER (PARTITION BY result ORDER BY dt) AS rn
from UserResult
)
select top 1 result, count(*) as number
from temp
group by result, rid - rn
order by count(*) desc
參考資料
如何對連續紀錄做分群